简体   繁体   English

从字符串中提取纬度/经度坐标

[英]Extract lat/lng coordinates from a string

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?将文本中的所有坐标提取到数组中的最佳方法是什么?

You could use match() here with an appropriate regex pattern:您可以在此处使用带有适当正则表达式模式的match()

 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.在上述字符串数组中分离出 lat/lng 对后,只需多做一点工作即可获得所需格式的单独数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM