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

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

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

1 Comment

This will remove hello in chello or hellois

Your Answer

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.