I want to build a search box in angular, that would return the array of item,and also highlight the searcchedTerm in the results array.
For example: In Chrome if you are searching any text, it would highlight with light yellow background. Similar to that.
I have created two pipes, one to filter the result and another to highlight the term in the results which is searched.
But i am getting error replace is not a function
.
And also, can the two pipes be merged into one?
highlight.pipe.ts
transform(list: any, searchText: string): any[] {
if (!list) { return []; }
if (!searchText) { return list; }
const re = new RegExp(searchText, 'gi');
const value = list.replace(re, "<span class='yellow'>" + searchText + "</span>" );
return list;
}
using the pipe in template
<div class="card" *ngFor="let item of list | search: searchedTerm | highlight: searchedTerm">