I would like to remove all occurrences of the words "hello" and "-hello" in a string, the "-" in "-hello" is a minus sign.
I am currently doing it as such
let regex = /hello/-hello/g
let word = "Hello, How are -hello you doing"
let newWord = word.replace(regex,'')
But this seems to throw an error, what is that correct way to do this?
thank you
adiga
35.4k9 gold badges66 silver badges88 bronze badges
1 Answer 1
Try this,
let regex = /-?hello/gi;
let word = "Hello, How are -hello you doing"
let newWord = word.replace(regex,'')
console.log(newWord);
georg
216k57 gold badges325 silver badges401 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Toto
This will remove
hello in chello or helloislang-js
regex = /-?\bhello\b/gregex = /(-?hello)/gi/instead of|. The/after hello closes the regex literal.