Skip to content

Instantly share code, notes, and snippets.

@chris-sev
Last active November 27, 2018 08:03
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 chris-sev/fd957d8935a577a6666e85ae9d34f00d to your computer and use it in GitHub Desktop.
Save chris-sev/fd957d8935a577a6666e85ae9d34f00d to your computer and use it in GitHub Desktop.
Replace All Instances of a Word
// 1. we have a sentence
const message = 'This is my store. In my store we store store store.'
// 2. replace all store with shop using a regular expression
// normally .replace() will only replace the 1st occurrence
const newMessage = message.replace(new RegExp('store', 'g'), 'shop');
// 3. output: This is my shop. In my shop we shop shop shop.
console.log(newMessage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment