I'm trying to write a set of filters to highlight and then dehighlight dynamically generated html:
Highlight filter:
app.filter('highlight', function ($sce) {
return function (str, termsToHighlight) {
// Sort terms by length
termsToHighlight.sort(function (a, b) {
return b.length - a.length;
});
var regex = new RegExp('(' + termsToHighlight.join('|') + ')', 'g');
return $sce.trustAsHtml(str.toString().replace(regex, '<span class="highlightedSpan" style="background-color: yellow; ' +
'font-weight: bold;">$&</span>'));
};
});
Dehighlight:
app.filter('dehighlight', function ($sce) {
return function (str) {
var obj = $($.parseHTML(str));
obj = obj.find('.highlightedSpan').replaceWith(function () { return this.innerHTML; });
return $sce.trustAsHtml(obj.html());
};
});
I'm trying to strip away the span tag and leave the original text, but I'm not sure if it's working or what to return. Str is the html string. Any help would be greatly appreciated!
angularjsis the correct tag for AngularJS 1.x. Theangulartag is for Angular 2+ only. Using the correct tags increases chances to get a good answer.