28

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,354
  • 1
  • 14
  • 26
HaBo
  • 13,307
  • 35
  • 108
  • 202

9 Answers9

45

Try the following:

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

A little bit of REGEX to grab those pesky parentheses.

René Schubert
  • 1,120
  • 1
  • 10
  • 30
George Reith
  • 12,742
  • 18
  • 77
  • 144
27

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
  • 72,900
  • 16
  • 109
  • 126
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,764
  • 19
  • 36
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
  • 223,102
  • 54
  • 301
  • 354
2

Just one replace will do:

"\"a(b)c'd{e}f[g]".replace(/[\(\)\[\]{}'"]/g,"")
nana
  • 4,353
  • 1
  • 32
  • 47
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
  • 14,381
  • 14
  • 52
  • 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
  • 23,106
  • 15
  • 65
  • 96
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,354
  • 1
  • 14
  • 26
Matt Fellows
  • 6,462
  • 4
  • 33
  • 55
  • 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