1

I'm trying to create a regex which will replace all the characters which are not in the specified white list (letters,digits,whitespaces, brackets, question mark and explanation mark)
This is the code :

var regEx = /^[^(\s|\w|\d|()|?|!|<br>)]*?$/;
    qstr += tempStr.replace(regEx, '');

What is wrong with it ?

Thank you

1
  • What do you intend to do with <br>s? Commented Jul 31, 2013 at 15:05

2 Answers 2

7
  • The anchors are wrong - they only allow the regex to match the entire string
  • The lazy quantifier is wrong - you wouldn't want the regex to match 0 characters (if you have removed the anchors)
  • The parentheses and pipe characters are wrong - you don't need them in a character class.
  • The <br> is wrong - you can't match specific substrings in a character class.
  • The \d is superfluous since it's already contained in \w (thanks Alex K.!)
  • You're missing the global modifier to make sure you can do more than one replace.
  • You should be using + instead of * in order not to replace lots of empty strings with themselves.

Try

var regEx = /[^\s\w()?!]+/g;

and handle the <br>s independently (before that regex is applied, or the brackets will be removed).

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

1 Comment

I think \w encompasses \d also
0

You'll want to use the g (global) modifier:

var regEx = /^[^(\s|\w|\d|()|?|!|<br>)]*?$/g; // <-- `g` goes there
qstr += tempStr.replace(regEx, '');

This allows your expression to match multiple times.

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.