为什么这个 javascript 正则表达式不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7427731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why this javascript regex doesn't work?
提问by J4N
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:
所以我做了以下正则表达式:
\(\s*([0-9.-]+)\s*,\s([0-9.-]+)\s*\)
I've tested it with RegexPaland 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 错误(使用 google chrome 的调试模式)。此代码托管在包含的 JS 文件中的 javascript 函数中。该方法在 OnLoad 方法中调用。
I've searched a lot, but I can't find why this isn't working. Thank you very much!
我已经搜索了很多,但我找不到为什么这不起作用。非常感谢你!
回答by Felix Kling
Use a regex literal [MDN]:
使用正则表达式[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 - 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)
(我想现在你明白为什么正则表达式更方便了)
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
字面上和括号()
仍然被视为特殊字符。
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 +
.
当然,如果您希望将值作为字符串而不是数字,则可以省略+
.