Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lettucebo/a33ad24c97e243b1cf1bf3d1bc60d963 to your computer and use it in GitHub Desktop.
Save lettucebo/a33ad24c97e243b1cf1bf3d1bc60d963 to your computer and use it in GitHub Desktop.
<script>
function highlight(container, what, spanClass) {
var content = container.innerHTML,
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)', 'g'),
replaceWith = '$1<span ' + (spanClass ? 'class="' + spanClass + '"' : '') + '">$2</span>$3',
highlighted = content.replace(pattern, replaceWith);
return (container.innerHTML = highlighted) !== content;
}
</script>
<style>
.highlight {
background-color: yellow;
}
.tab {
width: 200px;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment