0

How can I remove the parenthesis from this string in Javascript "(23,45)" ? I want it to be like this => "23,45" please!

user2447830
  • 53
  • 1
  • 2
  • 6
  • Let Google be your friend - I couldn't type fast enough to get an answer after searching "javascript string replace" – Brian Barnes Oct 25 '13 at 07:28

6 Answers6

18

Simply use replace with a regular expression :

str = str.replace(/[()]/g,'')

If you just wanted to remove the first and last characters, you could also have done

str = str.slice(1,-1);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Dude thanks! It worked perfectly with not even the slightest problem. – user2447830 Oct 26 '13 at 18:45
  • I was confused about the "-1" for a second. Looked it up - apparently if you use negative numbers with string.slice, the number will count from the end of the string instead of the start. – VSO May 05 '16 at 16:22
2
str = str.split("(").split(")").join();
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
2

You can use replace function

var a = "(23,45)";
a = a.replace("(","").replace(")","")
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

If they're always the first and last characters:

str = str.substr(1, str.length-2);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Try

"(23,45)".replace("(","").replace(")","")
1

Use this regex

var s = "(23,45)";
alert(s.replace(/[^0-9,]+/g,''))
Noman ali abbasi
  • 539
  • 2
  • 13