I have search results returned from the server and a regex that highlights the result using words in the search query.
words = searchQuery.split(' ');
$.each(words, function() {
var word = this.trim();
var regex = new RegExp('(' + word + ')(?!>|b>)', 'gi');
searchResult = searchResult.replace(regex, "<b>$1</b>");
});
This works fine, until I search something like this:
Search term: "script with javascript"
Search result: "javascript is a programming language..."
It should highlight the whole word like "javascript is a programming language...". However, since the string has changed from "javascript" to "java<b>script</b>", it no longer matches the second word in the search query. It also produces weird values when the value of word is "b", "/", "<" or ">".
My question is how I can ignore the <b> and </b> tags in the regex and match only the original search query? i tried using lookahead, but it didn't work.