0

I'm trying to do a replace for every pattern that looks like either:

" - ", // space dash space
" ", // space
"*", // asterisk

and replace it with a space.

I tried:

const name = "Test - number one*"
const regExName = name.replace((\s-\s)|(-)|(\s)|(\*)/g, '_');

Obviously wrong... Help please?

1
  • 1
    .replace(/ - |-| |\*/g, '_') Commented Jul 5, 2016 at 13:08

2 Answers 2

2

Simply use this pattern:

/ - | |\*/g

Online Demo


And here is full code:

const name = "Test - number one*"
const regExName = name.replace(/ - | |\*/g, '_');
document.write(regExName);

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

Comments

1

You are missing the leading /:

name.replace(/(\s-\s)|(-)|(\s)|(\*)/g, '_');
             ^ here

Comments

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.