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, '')
ORstr.replace(/[()]/g, '')
ORstr.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
2 Answers
Reset to default 4You 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条)