Skip to content

Instantly share code, notes, and snippets.

@tobie
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobie/ee9f00d6cefc35f104f7 to your computer and use it in GitHub Desktop.
Save tobie/ee9f00d6cefc35f104f7 to your computer and use it in GitHub Desktop.
Search term highlighter for well-formed HTML content.
function highlight(str, searchString) {
var regexp = new RegExp("(<[^>]+>)|(" + searchString + ")", "gi");
return (str || "").replace(regexp, function wrap(_, tag, txt) {
if (tag) return tag;
return "<strong class=\"highlight\">" + txt + "</strong>";
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment