Skip to content

Instantly share code, notes, and snippets.

@sirlancelot
Created June 29, 2012 17:25
Show Gist options
  • Select an option

  • Save sirlancelot/3019382 to your computer and use it in GitHub Desktop.

Select an option

Save sirlancelot/3019382 to your computer and use it in GitHub Desktop.
Highlight words in an element
// Highlight words containing github.
$("#content p").highlight("github");
// Small plugin to highlight a word with a class name
jQuery.fn.highlight = function(Str, ClassName) {
ClassName = ClassName || "highlight";
var regex = new RegExp("(" + Str + ")", "gi");
return this.each(function() {
this.innerHTML = this.innerHTML.replace(regex, "<span class=\""+ClassName+"\">$1</span>");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment