Javascript Highlight text

http://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript


$.fn.hl = function (word) {
 str = $(this).html();
 
 word="(\\b"+ word.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1")+ "\\b)";
 // var r = /(\w+)/ig
 // var r = new RegExp('(^|\\s|\\(|\'|"|,|;)' + 'chi' + '($|\\s|\\)|\\.|\'|"|!|,|;|\\?)', 'gi');;
 var r = new RegExp(word,"igm");
 str = str.replace(/(>[^<]+<)/igm,function(a){
  return a.replace(r,"$1");
 });
 $(this).html(str);
}
Sử dụng: $('.pagetitle').hl('text');
Javascript Highlight text

Bài đăng phổ biến từ blog này

Đệ quy trong MIPS