Reputation: 111
I've got a string which contains several coordinates. It is just random human readable text. For example:
Bla bla bla 35.45672,-162.91938 yadda yadda yadda -6.53197, 132.07407 bla bla
I would like to extract all the coordinates from the text to an array. The coordinates are seperated by a comma, or a comma and a space (As in the example above). There is no fixed length or seperators. There is no fixed amount of coordinates. It is just random text containing lat/lng coordinates.
What would be the best way to extract all coordinates from the text into an array?
Upvotes: 0
Views: 384
Reputation: 520978
You could use match()
here with an appropriate regex pattern:
var input = "Bla bla bla 35.45672,-162.91938 yadda yadda yadda -6.53197, 132.07407 bla bla";
var matches = input.match(/-?\d+(?:\.\d+)?,\s*-?\d+(?:\.\d+)?/g);
console.log(matches);
Once you have isolated the lat/lng pairs in the above string array, it is only a bit more work to obtain separate numbers in whatever format you need.
Upvotes: 2