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?
"(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
substr
, var str = '(12.131883, -68.84942999999998)'; str.substr(1, str.length -2);
– Tushar
Commented
Oct 9, 2015 at 20:13
str.replace(/\(|\)/g, '')
OR str.replace(/[()]/g, '')
OR str.match(/[^()]/g);
Choose whichever you like
– Tushar
Commented
Oct 9, 2015 at 20:19
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"]