Last active
August 29, 2015 14:11
-
-
Save tobie/ee9f00d6cefc35f104f7 to your computer and use it in GitHub Desktop.
Search term highlighter for well-formed HTML content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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