Javascript - extract specific numbers from string
I'm trying to extract specific numbers from a string, but I'm not sure how to accomplish it.
The line looks like:
center=43.571464,7.129565&zoom=12&size=480x225&markers=color:red%7Clabel:1%7C43.580293713725936,7.115145444335894&markers=color:red%7Clabel:2%7C43.56512073056565,7.121668576660113&sensor=false
The array I want is the coordinates of the marker near the end, specifically:
[43.580293713725936,7.115145444335894,43.56512073056565,7.121668576660113]
I thought I could select these numbers using their precision (15), but I don't know how much better it is. I swear when it comes to using regular expressions. Right now the best I have is:
str.match(/[-+]?[0-9]*\.?[0-9]+/g)
But that just gives me all the numbers.
Help rate!
source to share
You can try using the following regex
/\d+\.\d{7,}/g
This assumes that:
- Marker coordinates always have 7 or more numbers after point
- No other part of the line contains a similar pattern with more than 7 numbers after the dot
Example ( JSFiddle ):
str.match(/\d+\.\d{7,}/g);
The reason I chose 7 was because the other numbers in the sample had 6, so it excludes them. If you know that coordinates always have a fixed number of decimal places, then you can simply use that specific number without, ,
like this:
/\s+\.\d{10}/g
source to share