0

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
asked Apr 12, 2021 at 6:13
3
  • 2
    Try regex = /-?\bhello\b/g Commented Apr 12, 2021 at 6:15
  • 1
    Or regex = /(-?hello)/gi Commented Apr 12, 2021 at 6:18
  • 1
    The error is thrown because you're using / instead of |. The / after hello closes the regex literal. Commented Apr 12, 2021 at 6:18

1 Answer 1

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
answered Apr 12, 2021 at 6:22
Sign up to request clarification or add additional context in comments.

1 Comment

This will remove hello in chello or hellois

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.