我正在开发一个工具来将提供的JSON转换成一种不同的格式,所以我可以将它与传单热地图插件一起使用。现在的问题是转换整数。我想要实现的是:
Positive Longitudes
1717485672 -> 171.7485672
115855930 -> 11.5855930
99811421 -> 9.9811421
Negative Longitudes
-99811421 -> -9.9811421到目前为止我尝试过的是:
var longitude = "99811421";
longitude.replace(/(^([2-9]|1[0-9]))/, '$1.');但是,这只适用于20000000到190000000之间的范围。这对德国来说已经足够了,但对整个世界来说还不够;)
知道怎么做吗?
发布于 2014-08-30 21:56:35
如果您正在寻找正则表达式,可以使用以下命令:
^(?(?=\d{10}\b)
(\d{3})|
(?(?=\d{9}\b)
(\d{2})|
(?(?=\d{8}\b)
(\d{1}))))工作演示

作为一种解释,这个正则表达式是一个IF条件的链。在伪码中是:
if match 10 numbers then capture the first 3
else if match 9 numbers then capture the first 2
else if match 8 numbers then capture the first 1就我个人而言,我会使用另一种解决方案,而不是正则表达式,但是如果您想要正则表达式,可以使用这种方法。
https://stackoverflow.com/questions/25587005
复制相似问题