为什么这个javascript正则表达式不起作用? [英] Why this javascript regex doesn't work?

查看:122
本文介绍了为什么这个javascript正则表达式不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小的javascript方法,它接收一个点列表,我将阅读这些点以在谷歌地图中创建一个多边形。

I'm doing a small javascript method, which receive a list of point, and I've to read those points to create a Polygon in a google map.

我在表格上收到了以下内容:

I receive those point on the form:

(lat,long),(lat,long),(lat,long)

(lat, long), (lat, long),(lat, long)

所以我做了以下正则表达式:

So I've done the following regex:

\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)

我已经使用 RegexPal 和我收到的确切数据对其进行了测试:

I've tested it with RegexPal and the exact data I receive:

(25.774252, -80.190262),(18.466465, -66.118292),(32.321384, -64.75737),(25.774252, -80.190262)

并且它有效,所以为什么当我在我的javascript中使用此代码时,我在结果中收到null ?

and it works, so why when I've this code in my javascript, I receive null in the result?

var polygons="(25.774252, -80.190262),(18.466465, -66.118292),(32.321384, -64.75737),(25.774252, -80.190262)";
var reg = new RegExp("/\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)/g");
var result = polygons.match(reg);

我执行时没有javascript错误(使用谷歌浏览器的调试模式)。此代码托管在javascript函数中,该函数位于包含的JS文件中。在OnLoad方法中调用此方法。

I've no javascript error when executing(with debug mode of google chrome). This code is hosted in a javascript function which is in a included JS file. This method is called in the OnLoad method.

我搜索了很多,但我找不到为什么这不起作用。非常感谢!

I've searched a lot, but I can't find why this isn't working. Thank you very much!

推荐答案

使用 regex literal [MDN]

var reg = /\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)/g;

使用 RegExp [MDN]


  • 分隔符 / 不应该是表达式的一部分

  • 如果将表达式定义为字符串,则必须转义反斜杠,因为它是字符串中的转义字符

  • The "delimiters" / are should not be part of the expression
  • If you define an expression as string, you have to escape the backslash, because it is the escape character in strings

此外,修饰符作为第二个参数传递给函数。

Furthermore, modifiers are passed as second argument to the function.

因此,如果你想使用 RegExp (在这种情况下你不必),等价物是:

So if you wanted to use RegExp (which you don't have to in this case), the equivalent would be:

var reg = new RegExp("\\(\\s*([0-9.-]+)\\s*,\\s([0-9.-]+)\\s*\\)", "g");

(我想你现在看到为什么正则表达式文字更方便了)

(and I think now you see why regex literals are more convenient)

我总是觉得在控制台中复制并通过 RegExp 表达式会很有帮助输出。采用原始表达式,我们得到:

I always find it helpful to copy and past a RegExp expression in the console and see its output. Taking your original expression, we get:

/(s*([0-9.-]+)s*,s([0-9.-]+)s*)/g

表示表达式尝试匹配 / s g 字面意思和parens ()仍被视为特殊字符。

which means that the expressions tries to match /, s and g literally and the parens () are still treated as special characters.

更新: .match()返回一个数组:

["(25.774252, -80.190262)", "(18.466465, -66.118292)", ... ]

似乎不是很有用。

您必须使用 .exec() [MDN] 提取数字:

You have to use .exec() [MDN] to extract the numbers:

["(25.774252, -80.190262)", "25.774252", "-80.190262"]

必须重复调用,直到整个字符串被处理完毕。

This has to be called repeatedly until the whole strings was processed.

示例:

var reg = /\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)/g;
var result, points = [];

while((result = reg.exec(polygons)) !== null) {
    points.push([+result[1], +result[2]]);
}

这会创建一个数组数组和一元加号( + )将字符串转换为数字:

This creates an array of arrays and the unary plus (+) will convert the strings into numbers:

[
    [25.774252, -80.190262], 
    [18.466465, -66.118292], 
    ...
]

当然,如果您希望将值设置为字符串而不是数字,则可以省略 +

Of course if you want the values as strings and not as numbers, you can just omit the +.

这篇关于为什么这个javascript正则表达式不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆