最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, removing brackets from string - Stack Overflow

matteradmin3PV0评论

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"]

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far