Hightlight words in free text using jQuery

With the method addClass() and an CSS class it is easy to change the appearance of the text with the limitation that the text has to appear in HTML tags. For highlighting specific words in free text, this method is not working.
1 answer

Highlighting words in free text using jQuery

You can use jQuery methods for highlighting specific words in free text.

The most sophisticated way to solve the problem was the use of a regular expression. Every appearance of the searchword will be surrounded with an span tag.

$('p:contains('+str+')').
each(function(){
var regex = new RegExp(str, "g");
$(this).html($(this).html().replace( regex ,""+str+""));
});

Taggings: