I would like to leave a comment but stack doesn't allow me or I can't seem to find a button for it so have to do it in an answer.
There is a problem with the script.
for example:
Highlighting fox in the following string:
<img src="brown fox.jpg" title="The brown fox" /><p>some text containing fox.</p>
Would break the img tag:
var term = "fox";
term = term.replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*");
var pattern = new RegExp("("+term+")", "i");
src_str ='<img src="brown fox.jpg" title="The brown fox" />'
+'<p>some text containing fox.</p>'
src_str = src_str.replace(pattern, "<mark>$1</mark>");
src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4");
src_str
I was working on a highlight script and solved that problem not realizing the script might have to highlight over multiple tags.
Here is how I got it not to destroy tags by trying to highlight content within tags, this script highlights multiple instances within content as well but not over multiple tags:
str='<img src="brown fox.jpg" title="The brown fox" />'
+'<p>some text containing fox. And onother fox.</p>'
var word="fox";
word="(\\b"+
word.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1")
+ "\\b)";
var r = new RegExp(word,"igm")
str.replace(/(>[^<]+)/igm,function(a){
return a.replace(r,"<span class='hl'>$1</span>");
})
Not sure how to combine these scripts and make a highlight over multiple tags work but will keep my eye on this thread.