在JavaScript中,如何从字符串中提取经度和纬度 [英] In JavaScript, How to extract latitude and longitude from string
问题描述
我正在从数据库中提取一个字符串,该字符串需要分别解析为纬度和经度, 该字符串被定义为点",格式如下:
I am extracting a string from a database which needs to be parsed into latitude and longitude separately, The string is defined as a "point", in the following format:
(2.340000000,-4.50000000)
我试图删除括号,然后使用方法split()
对其进行解析,但是我无法提出能正确完成工作的正则表达式:
I am trying to remove the parenthesis, and then parsed them with the method split()
but I haven't been able to come up with a regular expressions that does the job right:
到目前为止,我已经尝试了许多替代方法,并且
So far I have tried many alternatives, and
var latlong = "(2.34000000, -4.500000000)"
latlong.replace('/[\(\)]//g','');
var coords = latlong.split(',');
var lat = coords[0];
var long = coords[1];
如果我运行它,我得到的是:
NaN, -4.500000
If I run that, all I got is:
NaN, -4.500000
我做错了什么? 谢谢!
What am I doing wrong? Thanks!
推荐答案
似乎可以正常工作,但是你多了一个斜杠
Seems to work, but you had an extra slash
var value = '(2.340000000,-4.50000000)';
//you had an extra slash here
// ''''''''''''''''v
value = value.replace(/[\(\)]/g,'').split(',');
console.log(value[0]);
console.log(value[1]);
这篇关于在JavaScript中,如何从字符串中提取经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!