jQuery - Replace all parentheses in a string

61,348

Solution 1

Try the following:

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

A little bit of REGEX to grab those pesky parentheses.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

Just one replace will do:

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

Related videos on Youtube

HaBo
Author by

HaBo

I am a Microsoft Certified Solution Developer, working as a “Director/Senior Application Architect/Developer” with an overall experience of 10+ Years in related fields. I have a tremendous desire to exceed in whatever I undertake.

Updated on March 07, 2020

Comments

  • HaBo
    HaBo about 4 years

    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?

    • rickyduck
      rickyduck about 12 years
      This isnt jQuery, its just standard javascript
    • HaBo
      HaBo about 12 years
      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?
  • Matt Fellows
    Matt Fellows about 12 years
    Do you not need to escape the parentheses?
  • struppi
    struppi about 12 years
    But there's no concept of "group" for the non-regex version of replace that the OP is using.
  • Joseph Marikle
    Joseph Marikle about 12 years
    @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)
  • Joseph Marikle
    Joseph Marikle about 12 years
    @MattFellows I will add however that the exceptions to this are [, ], and `\` which do need escaped.
  • Matt Fellows
    Matt Fellows about 12 years
    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.
  • nana
    nana about 12 years
    @JosephMarikle I learn something new every day, thanks! I had no clue you don't have to escape them.
  • struppi
    struppi about 12 years
    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.
  • Matt Fellows
    Matt Fellows about 12 years
    Duly noted updating to make it explicit