Alvin Bakker
Alvin Bakker

Reputation: 1526

jQuery, removing brackets from string

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?

Upvotes: 2

Views: 6167

Answers (2)

taxicala
taxicala

Reputation: 21759

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(')'));

Upvotes: 4

j08691
j08691

Reputation: 207901

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

Upvotes: 1

Related Questions