0

Here is my regular expression : I want to replace string with empty value so that only unwanted characters left. Somehow it is returning the same string, not replacing any character defined in regular expression:

var regExpress = /^([a-zA-Z0-9!#%^*()\-\_+=|}{'";:\/.,\s]*)$/gi;
strMessage = strMessage.replace(regExpress, '');

what am I doing wrong ? I know it should be simple but unable to figure it out .

1
  • If the message is unchanged then the regex do not match. Can you provide examples ? Commented Mar 5, 2012 at 16:34

2 Answers 2

3

Remove ^ and $. These are the markers of the beginning and end of the string.

Without these, your method will only replace the characters when the whole string contains the characters as specified in the pattern.

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

Comments

2

If I understand you correctly, you want to replace all the characters specified in the regex with the empty string ''?

In that case, you have a lot of cruft in your regex. Try this regex:

/[a-zA-Z0-9!#%^*()\-\_+=|}{'";:\/.,\s]/gi

Even that could be simplified with e.g. \w for a-zA-Z0-9_.

/[\w!#%^*()\-+=|}{'";:\/.,\s]/gi

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.