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?

asked Jul 5, 2016 at 13:05
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);

answered Jul 5, 2016 at 13:07
1

You are missing the leading /:

name.replace(/(\s-\s)|(-)|(\s)|(\*)/g, '_');
 ^ here
answered Jul 5, 2016 at 13:07

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.