import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'mwHighlight' }) export class MwHighlightPipe implements PipeTransform { transform(text: string, searchText: string): string { if ( !text || !searchText || typeof searchText !== 'string' || searchText.length <= 0 ) { return text; } const regexpString = `(${searchText .split(' ') .map((t) => t.trim().replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) .join('|')})`; const regex = new RegExp(regexpString, 'gi'); return text.replace(regex, (str) => `${str}`); } }