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/,"")
1
  • You missed the global flag g that regexr includes by default Commented Sep 23, 2019 at 20:27

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.