为什么这个javascript regex不能工作?

时间:2021-12-14 21:44:04

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.

我正在做一个小的javascript方法,它接收一个点列表,我必须读取这些点,在谷歌映射中创建一个多边形。

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:

我做了如下regex:

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

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

我用RegexPal和我收到的确切数据进行了测试:

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

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

它是有效的,为什么javascript中有这些代码,结果就会是null呢?

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);

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.

执行时没有javascript错误(使用谷歌chrome的调试模式)。此代码驻留在包含在JS文件中的javascript函数中。在OnLoad方法中调用此方法。

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

我找了很多,但是我找不出为什么它不能工作。非常感谢!

1 个解决方案

#1


14  

Use a regex literal [MDN]:

使用regex文字[MDN]:

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

You are making two errors when you use RegExp [MDN]:

当使用RegExp [MDN]时,您正在犯两个错误:

  • The "delimiters" / are should not be part of the expression
  • “分隔符”/ are不应该是表达式的一部分
  • 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.

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

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

所以如果你想要使用RegExp(在这种情况下你不需要这样做),等价的是:

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)

(我想你现在明白为什么regex文字更方便了)


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:

我总是发现,复制并通过控制台中的RegExp表达式并查看它的输出是很有帮助的。根据你最初的表达,我们得到:

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

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

这意味着表达式试图匹配/、s和g,而parens()仍然被视为特殊字符。


Update: .match() returns an array:

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

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

which does not seem to be very useful.

这似乎不是很有用。

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

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

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

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

这必须被反复调用,直到整个字符串被处理。

Example:

例子:

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 +.

当然,如果您希望值作为字符串而不是数字,您可以省略+。

#1


14  

Use a regex literal [MDN]:

使用regex文字[MDN]:

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

You are making two errors when you use RegExp [MDN]:

当使用RegExp [MDN]时,您正在犯两个错误:

  • The "delimiters" / are should not be part of the expression
  • “分隔符”/ are不应该是表达式的一部分
  • 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.

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

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

所以如果你想要使用RegExp(在这种情况下你不需要这样做),等价的是:

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)

(我想你现在明白为什么regex文字更方便了)


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:

我总是发现,复制并通过控制台中的RegExp表达式并查看它的输出是很有帮助的。根据你最初的表达,我们得到:

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

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

这意味着表达式试图匹配/、s和g,而parens()仍然被视为特殊字符。


Update: .match() returns an array:

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

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

which does not seem to be very useful.

这似乎不是很有用。

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

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

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

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

这必须被反复调用,直到整个字符串被处理。

Example:

例子:

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 +.

当然,如果您希望值作为字符串而不是数字,您可以省略+。