3

I'm trying to extract specific numbers from a string but I'm not sure how to execute it.

The string is of the form:

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 marker coordinates near the end, specifically:

[43.580293713725936,7.115145444335894,43.56512073056565,7.121668576660113]

I thought I could pick these number out using their precision (15) but I don't know if that's best. I'm a hack when it comes to using regular expressions. Right now the best I've got is:

str.match(/[-+]?[0-9]*\.?[0-9]+/g)

But that just gives me all of the numbers.

Help much appreciated!

2 Answers 2

1

If your string is in str use this regex.

var coordinates = decodeURIComponent(str).match(/([\d.]{10,})/g); 

http://jsfiddle.net/CHfcT/

Sign up to request clarification or add additional context in comments.

Comments

1

You could try using the following regex

/\d+\.\d{7,}/g

This assumes that:

  • The marker coordinates always have 7 or more numbers after the dot
  • No other part of the string contains a similar pattern with more than 7 numbers after a dot

Example (JSFiddle):

str.match(/\d+\.\d{7,}/g);

The reason I picked 7 was because the other numbers in the sample had 6, so that excludes them. If you know that the coordinates always have a fixed number of decimal places, then you could just use that specific number without the , like this:

/\s+\.\d{10}/g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.