0

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?

4
  • Why is it a string in the first place? Commented Aug 15, 2012 at 11:36
  • try reading any introductory book to Java, first..? Commented Aug 15, 2012 at 11:36
  • 3
    @Alnitak why - so he can then become a Java programmer, earn some money, and buy a JavaScript book? ;) Commented Aug 15, 2012 at 11:36
  • 1
    JavaScript documentation is your friend. There are plenty of String methods that will help you do this. Commented Aug 15, 2012 at 11:42

2 Answers 2

2

Using regular expressions you can do /^LatLng\((\d+\.\d+), (\d+\.\d+)\)$/.

With javascript:

console.log('LatLng(41.3371, 19.81717)'.match(/^LatLng\((\d+\.\d+), (\d+\.\d+)\)$/)[1]);​
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.
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?
Thanks Evgeiny. Eventually, I used another geolocation function to extract only the coordinate values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.