javascript - jQuery, removing brackets from string - Stack Overflow

I have a string like (12.131883, -68.84942999999998) and with using .replace()I wish to remove the br

I have a string like (12.131883, -68.84942999999998) and with using .replace() I wish to remove the brackets, or get the values between the brackets. A simple latlon = latlon.replace('(',' '') is not working.

Also tried it with latlon.replace(/\(.*?\)\s/g, '') but no luck.

Can anyone help me out here?

I have a string like (12.131883, -68.84942999999998) and with using .replace() I wish to remove the brackets, or get the values between the brackets. A simple latlon = latlon.replace('(',' '') is not working.

Also tried it with latlon.replace(/\(.*?\)\s/g, '') but no luck.

Can anyone help me out here?

Share Improve this question asked Oct 9, 2015 at 20:07 Alvin BakkerAlvin Bakker 1,5361 gold badge21 silver badges41 bronze badges 5
  • How is "(12.131883, -68.84942999999998)".replace('(', ''); not working ? Any error ? I just tried it in the console and it works just fine.. – Nico Commented Oct 9, 2015 at 20:10
  • 2 Use substr, var str = '(12.131883, -68.84942999999998)'; str.substr(1, str.length -2); – Tushar Commented Oct 9, 2015 at 20:13
  • 1 OR str.replace(/\(|\)/g, '') OR str.replace(/[()]/g, '') OR str.match(/[^()]/g); Choose whichever you like – Tushar Commented Oct 9, 2015 at 20:19
  • Tushar, many thanx. Something this stupid I overlooked. – Alvin Bakker Commented Oct 9, 2015 at 20:27
  • @AlvinBakker Glad to help :) – Tushar Commented Oct 9, 2015 at 20:31
Add a ment  | 

2 Answers 2

Reset to default 4

You can use substring:

var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(1, myString.indexOf(')'));

Or:

var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(myString.indexOf('(') + 1, myString.indexOf(')'));

You can get them in an array with:

var latlon = "(12.131883, -68.84942999999998)";
var ll = latlon.match(/[-+]?[0-9]*\.?[0-9]+/g)
console.log(ll) // returns ["12.131883", "-68.84942999999998"]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745036862a4607572.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信