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?

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
HaBo
  • 13,999
  • 36
  • 114
  • 206

9 Answers9

51

Try the following:

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

A little bit of REGEX to grab those pesky parentheses.

René Schubert
  • 1,302
  • 2
  • 13
  • 31
George Reith
  • 13,132
  • 18
  • 79
  • 148
29

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.

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
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.

Sam Greenhalgh
  • 5,952
  • 21
  • 37
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.

jAndy
  • 231,737
  • 57
  • 305
  • 359
2

Just one replace will do:

"\"a(b)c'd{e}f[g]".replace(/[\(\)\[\]{}'"]/g,"")
nana
  • 4,426
  • 1
  • 34
  • 48
0

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

user3745828
  • 82
  • 1
  • 7
0

That should work :

mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");
David Laberge
  • 15,435
  • 14
  • 53
  • 83
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.

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
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, "");
fragilewindows
  • 1,394
  • 1
  • 15
  • 26
Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
  • But there's no concept of "group" for the non-regex version of `replace` that the OP is using. – Chris Farmer Feb 02 '12 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. – Matt Fellows Feb 02 '12 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 `replace`s to regex-based `replace`s. – Chris Farmer Feb 02 '12 at 16:11
  • Duly noted updating to make it explicit – Matt Fellows Feb 02 '12 at 16:29