1 import { Pipe, PipeTransform } from '@angular/core';
4 name: 'searchHighlight'
6 export class SearchHighlightPipe implements PipeTransform {
7 transform(value: string, args: string): string {
11 args = this.escapeRegExp(args);
12 const regex = new RegExp(args, 'gi');
13 const match = value.match(regex);
19 return value.replace(regex, '<mark>$&</mark>');
22 private escapeRegExp(str: string) {
23 // $& means the whole matched string
24 return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');