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

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

问题描述

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

我在表格上收到了这些要点:

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

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

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

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

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

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

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

我在执行时没有 javascript 错误(使用 google chrome 的调试模式).此代码托管在包含的 JS 文件中的 javascript 函数中.该方法在 OnLoad 方法中调用.

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

解决方案

使用 正则表达式 [MDN]:

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

您在使用 RegExp [MDN]:

  • 分隔符"/ 不应成为表达式的一部分
  • 如果将表达式定义为字符串,则必须对反斜杠进行转义,因为它是字符串中的转义字符

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

因此,如果您想使用 RegExp(在这种情况下您不必使用),则等效于:

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

(我想现在你明白为什么正则表达式更方便了)

<小时>

我总是发现在控制台中复制和粘贴 RegExp 表达式并查看其输出很有帮助.用你的原始表达,我们得到:

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

这意味着表达式试图匹配 /sg 字面以及括号 ()仍被视为特殊字符.

<小时>

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

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

这似乎不是很有用.

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

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

这必须重复调用,直到处理完整个字符串.

示例:

var reg =/(s*([0-9.-]+)s*,s([0-9.-]+)s*)/g;var 结果,点数 = [];while((result = reg.exec(polygons)) !== null) {点推([+结果[1],+结果[2]]);}

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

<预><代码>[[25.774252, -80.190262],[18.466465, -66.118292],...]

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

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)

So I've done the following regex:

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

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)

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

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!

解决方案

Use a regex literal [MDN]:

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

You are making two errors when you use 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:

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:

/(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.


Update: .match() returns an array:

["(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:

["(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 +.

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

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