浏览器对具有前瞻功能的正则表达式有不同的解释
我正在使用 /\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试拆分
/\b(?:AND|OR)\b/
,并修剪生成的部分。请注意,布尔运算符具有优先级规则,您不能仅在
AND
和OR
上进行拆分而不失去意义。此外,布尔表达式(理论上)可以括在嵌套括号中,这基本上排除了正则表达式作为解析它们的技术。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
andOR
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.这将返回您想要的结果:
This will return the result you want:
看起来 Firefox 和 Chrome 完全正确,因为根据 ECMAScriptv5 第 15.5.4.14 节的规范
指向 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
Pointer to the specs by Chris Leary of Mozilla.