JavaScript:好的部分;为什么前瞻不好?
我正在阅读 Douglas Crockfords Javascript:好的部分,我刚刚读完正则表达式章节。在本章中,他称 JavaScript 的 \b
、正向先行 (?=)
和负向先行 (?!)
“不是一个好的选择。
他解释了 \b
不好的原因(它使用 \w
进行字边界查找,并使用 \w 对于任何使用 unicode 字符的语言都会失败),这对我来说似乎是一个很好的理由。
不幸的是,正向和负向前瞻不好的原因被遗漏了,我也想不出一个原因。 掌握正则表达式向我展示了前瞻的力量(当然也解释了它带来的问题),但我真的想不出任何可以将其定义为“不是一个好部分”的东西。
任何人都可以解释为什么JavaScript(正|负)先行或一般的(正|负)先行应该被认为“不好”吗?
I'm reading Douglas Crockfords Javascript: The Good Parts, I just finished the regular expressions chapter. In this chapter he calls JavaScript's \b
, positive lookahead (?=)
and negative lookahead (?!)
"not a good part"
He explains the reason for \b
being not good (it uses \w
for word boundary finding, and \w
fails for any language that uses unicode characters), and that looks like a very good reason to me.
Unfortunately, the reason for positive and negative lookahead being not good is left out, and I cannot come up with one. Mastering Regular Expressions showed me the power that comes with lookahead (and of course explains the issues it brings with it), but I can't really think of anything that would qualify it as "not a good part".
Can anyone explain why JavaScript (positive|negative) lookahead or (positive|negative) lookahead in general should be considered "not good"?
It seems I'm not the only one with this question: one and two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是因为 Internet Explorer 的永远存在错误的先行实现。对于任何撰写有关 JavaScript 的书籍的人来说,任何在 IE 中不起作用的功能都可能不存在。
Maybe it's because of Internet Explorer's perpetually buggy implementation of lookaheads. For anyone authoring a book about JavaScript, any feature that doesn't work in IE might as well not exist.
我能想到的唯一原因可能是它们仅被大约一半的流行正则表达式引擎支持,尽管如果您将自己限制在普遍支持的语法上,那么您将无法做很多事情。
顺便说一下,(正|负)(lookahead|lookbehind)有时统称为“lookaround”,如本页中比较各种实现之间的功能支持:
http://www.regular-expressions.info/refflavors.html
The only reason I can think of might be that they are only supported by about half of the popular regular expression engines, though if you limit yourself to universally supported syntax there are a lot of things you just cannot do.
By the way (positive|negative)(lookahead|lookbehind) is sometimes collectively referred to as "lookaround", as in this page that compares the support of features among various implementations:
http://www.regular-expressions.info/refflavors.html
他们对他来说太难了?
或者:向前查找和向后查找(JavaScript 不支持后者)会大大增加正则表达式的时间。但人们通常不会在 JavaScript 中对大量数据进行正则化。所以他们很棒;当它们有用时使用它们。
They're too hard for him?
Or: lookaheads and lookbehinds (the latter are not supported in JavaScript) increase regex times considerably. But one isn't typically regexing through huge amounts of data in JavaScript. So they're great; use them when they're useful.