浏览器对具有前瞻功能的正则表达式有不同的解释

发布于 2024-09-14 01:24:21 字数 1366 浏览 2 评论 0原文

我正在使用 /\s+(AND|OR)(?=\s+")\s+/ 在 javascript 中运行拆分 现在

"email" IS NOT NULL AND "email" LIKE '%gmail.com' OR "email" = '[email protected]'

,我对正则表达式的理解将使我期望获得以下数组:

[0]: "email" IS NOT NULL
[1]: "email" LIKE '%gmail.com'
[2]: "email" = '[email protected]'

注意:为了清楚起见,我去掉了分隔符,

我得到了

[0]: "email" IS NOT NULL
[1]:  AND
[2]: "email" LIKE '%gmail.com'
[3]:  OR
[4]: "email" = '[email protected]'

在 OS X 10.6.4 上运行 Firefox 3.6.8、Chrome 5.0.375.126 和 Safari 5.0.1 时,

但是,当我 。使用默认设置的 IE8 8.0.6 和我最初所期望的 PHP 5.2.10 也以这种方式分割它,

我的猜测是,这一次“好”浏览器得到了它。错误,但我想要更多意见。

编辑:我在这里给出的电子邮件示例是一个幼稚的示例。"xyz" = ' 1' AND "zyx" = 'test AND toast' 是另一个可能的输入字符串,

我所知道的结构是整个字符串将具有以下模式:

"<attribute>" <operator> '<value>'( (AND|OR) "<attribute>" <operator> '<value>')*

注意:空格实际上代表 \s+< /代码>

I am running a split in javascript with /\s+(AND|OR)(?=\s+")\s+/ on

"email" IS NOT NULL AND "email" LIKE '%gmail.com' OR "email" = '[email protected]'

Now, my understanding of regular expressions would lead me to expect obtaining the following array:

[0]: "email" IS NOT NULL
[1]: "email" LIKE '%gmail.com'
[2]: "email" = '[email protected]'

Note: I got rid of the delimiters for clarity.

However, I obtain

[0]: "email" IS NOT NULL
[1]:  AND
[2]: "email" LIKE '%gmail.com'
[3]:  OR
[4]: "email" = '[email protected]'

when running on Firefox 3.6.8, Chrome 5.0.375.126 and Safari 5.0.1 on OS X 10.6.4.

However, when I tried on an up to date IE8 8.0.6 with default settings and I obtain what I was expecting at first. PHP 5.2.10 with preg_split does also split it this way.

My guess is that for once the 'good' browsers got it wrong but I'd like more opinions.

Edit: The example I gave here with emails is a naive example. Basically I don't know what each member can be. "xyz" = '1' AND "zyx" = 'test AND toast' is another possible input string.

What I know of the structure is that the whole string will have the following pattern:

"<attribute>" <operator> '<value>'( (AND|OR) "<attribute>" <operator> '<value>')*

Note: spaces actually represent \s+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2024-09-21 01:24:21

尝试拆分 /\b(?:AND|OR)\b/,并修剪生成的部分。

请注意,布尔运算符具有优先级规则,您不能仅在 ANDOR 上进行拆分而不失去意义。此外,布尔表达式(理论上)可以括在嵌套括号中,这基本上排除了正则表达式作为解析它们的技术。

Try splitting on /\b(?:AND|OR)\b/, and trim the resulting parts.

Be aware that boolean operators have precedence rules and you cannot just split on AND and OR without losing meaning. Also, boolean expressions can (in theory) be enclosed in nested parentheses, which basically rules out regular expressions as a technology to parse them.

夏至、离别 2024-09-21 01:24:21

这将返回您想要的结果:

var string = "\"email\" IS NOT NULL AND \"email\" LIKE '%gmail.com' OR \"email\" = '[email protected]'"
string.split(/\s+(?:AND|OR)\s+/)

This will return the result you want:

var string = "\"email\" IS NOT NULL AND \"email\" LIKE '%gmail.com' OR \"email\" = '[email protected]'"
string.split(/\s+(?:AND|OR)\s+/)
情感失落者 2024-09-21 01:24:21

看起来 Firefox 和 Chrome 完全正确,因为根据 ECMAScriptv5 第 15.5.4.14 节的规范

如果分隔符是正则表达式
包含捕获括号,
然后每次分隔符都匹配
结果(包括任何未定义的
捕获括号的结果)
被拼接到输出数组中。

例如,

"A粗体编码".split(/<(\/)?([^<>]+)>/ )

计算数组

[“A”,未定义,“B”,“粗体”,“/”,
“B”,“和”,未定义,
“代码”,“编码”,“/”,“代码”,“”]

指向 Mozilla 的 Chris Leary 的规范。

It looks like Firefox and Chrome got it perfectly right, since according to the specs of ECMAScriptv5 section 15.5.4.14

If separator is a regular expression
that contains capturing parentheses,
then each time separator is matched
the results (including any undefined
results) of the capturing parentheses
are spliced into the output array.

For example,

"A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/)

evaluates to the array

["A", undefined, "B", "bold", "/",
"B", "and", undefined,
"CODE", "coded", "/", "CODE", ""]

Pointer to the specs by Chris Leary of Mozilla.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文