1

I need to remove 2 words from a string. The words are _with and _and so raised_hand_with_fingers_and_splayed becomes raised_hand_fingers_splayed

The regex /_with|_and/ appears to work in https://regexr.com/ but when I use it with JavaScript only the _with is removed:

const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/,"")
Evanss
  • 20,529
  • 79
  • 252
  • 460

1 Answers1

3

You need the g modifier to perform multiple replacements. Otherwise it just replaces the first match.

const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/g,"")
console.log(newStr);
Barmar
  • 669,327
  • 51
  • 454
  • 560