Variations of this have been asked before, but I can't find anything specific to what I'm looking for. I'm terrible with Regex and I figure my issue mostly lies there. I have a simple-ish Angular Directive (that I snagged online, never used Directives before) that highlights whatever string I searched for in my results from a MySQL stored procedure on the back-end. The problem is, it will only work if I search for one word.
For example if I search for "network" it will wrap every instance of the string "network" in a span with a class anywhere I point the directive to. However, if I search "network drive" it won't highlight anything because it's looking for that string specifically.
Here's the part of the directive that has this functionality:
getFormattedText() {
const re = new RegExp(`(${this.searchedWord})`, 'gi');
return this.content.replace(re, `<span class="${this.classToApply}">$1</span>`);
}
I was trying to make searchedWord an array of strings or a string and then apply the span element to each matched string but I ended up making things worse.
Any nudge in the right direction is much appreciated.
Thanks!