31

I tried this:

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace("(", "").replace(")", "");

It works for all double and single quotes but for parentheses, this only replaces the first parenthesis in the string.

How can I make it work to replace all parentheses in the string using JavaScript? Or replace all special characters in a string?

2
  • This isnt jQuery, its just standard javascript
    – rickyduck
    Feb 2, 2012 at 15:56
  • oops! hope you understand the requirement, i need to get this done either by jquery or standard javascript. would you be able to help me?
    – HaBo
    Feb 2, 2012 at 15:57

9 Answers 9

52

Try the following:

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(|\)/g, "");

A little bit of REGEX to grab those pesky parentheses.

30

You should use something more like this:

mystring = mystring.replace(/["'()]/g,"");

The reason it wasn't working for the others is because you forgot the "global" argument (g)

note that [...] is a character class. anything between those brackets is replaced.

4
  • 1
    Do you not need to escape the parentheses? Feb 2, 2012 at 15:59
  • 1
    @MattFellows not while it's in a character class. (I tend to put special characters in character classes just so I don't have to escape them... makes it more readable for me at least :P) Feb 2, 2012 at 16:00
  • 1
    @MattFellows I will add however that the exceptions to this are [, ], and `\` which do need escaped. Feb 2, 2012 at 16:02
  • @JosephMarikle I learn something new every day, thanks! I had no clue you don't have to escape them.
    – nana
    Feb 2, 2012 at 16:06
7

You should be able to do this in a single replace statement.

mystring = mystring.replace(/["'\(\)]/g, "");

If you're trying to replace all special characters you might want to use a pattern like this.

mystring = mystring.replace(/\W/g, "");

Which will replace any non-word character.

3

You can also use a regular experession if you're looking for parenthesis, you just need to escape them.

mystring = mystring.replace(/\(|\)/g, '');

This will remove all ( and ) in the entire string.

2

Just one replace will do:

"\"a(b)c'd{e}f[g]".replace(/[\(\)\[\]{}'"]/g,"")
0

That should work :

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");
0

That's because to replace multiple occurrences you must use a regex as the search string where you are using a string literal. As you have found searching by strings will only replace the first occurrence.

0

The string-based replace method will not replace globally. As such, you probably want to use the regex-based replacing method. It should be noted:

You need to escape ( and ) as they are used for group matching:

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");
4
  • But there's no concept of "group" for the non-regex version of replace that the OP is using. Feb 2, 2012 at 16:00
  • That is true - but to get global replacing you need to use a regex I believe - hence why I've sugested a regex, and then my statement is true. Feb 2, 2012 at 16:02
  • It's true, but it's just a bit misleading IMO since the grouping issue was not the cause of the OP's original problem. I just think it's worth a specific mention that you're converting his string-based replaces to regex-based replaces. Feb 2, 2012 at 16:11
  • Duly noted updating to make it explicit Feb 2, 2012 at 16:29
0

This can solve the problem: myString = myString.replace(/\"|\'|\(|\)/) Example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.