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
  • 355,860
  • 83
  • 755
  • 726
  • 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,434
  • 16
  • 54
  • 74
pythonian29033
  • 5,013
  • 5
  • 30
  • 56
2

You can use replace function

var a = "(23,45)";
a = a.replace("(","").replace(")","")
Satpal
  • 129,808
  • 12
  • 152
  • 166
1

If they're always the first and last characters:

str = str.substr(1, str.length-2);
Barmar
  • 669,327
  • 51
  • 454
  • 560
1

Try

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

Use this regex

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