I have a string like this LatLng(41.3371, 19.81717)stored in variable a
But I only need the number 41.3371 to make some further calculation.
Any idea on how I can extract the number?
2 Answers
Use regular expression like
<div id="res"></div>
<script type="text/javascript">
var res = document.getElementById('res'),
re = /\((\d+\.\d+),\s*(\d+\.\d+)\)/,
str = 'LatLng(41.3371, 19.81717)',
m = str.match(re);
res.innerHTML = 'x = ' + m[1] + ' and y = ' + m[2];
</script>
DEMO
3 Comments
multigoodverse
I am using the code above but instead of str = 'LatLng(41.3371, 19.81717)', I have to put str = a, where "a" is the variable containing the LatLng(41.3371, 19.81717) value. "a" is being assigned the value LatLng(41.3371, 19.81717) in a function inside the script.
Eugene Naydenov
So your
a variable and str variable are not strings, but an instance of LatLng? If yes, which value provides a.toString()? Is there a methods or properties to get a latitude and longitude directly from object?multigoodverse
Thanks Evgeiny. Eventually, I used another geolocation function to extract only the coordinate values.
Stringmethods that will help you do this.