Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created July 1, 2022 20:59
Show Gist options
  • Select an option

  • Save Octagon-simon/b78098acfe0126c9671cd176221a0d12 to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/b78098acfe0126c9671cd176221a0d12 to your computer and use it in GitHub Desktop.
let highlight = (word) => {
//get web page contents
let str = document.body.innerHTML.toString();
//search and replace
str = str.replaceAll( new RegExp(word, 'gi'), `<span style='background-color:red;color:#fff'>${word}</span>` );
//rebuild the webpage
document.body.innerHTML = str;
}
//invoke function
highlight("Simon");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment