JavaScript: The Good Parts; why is lookahead not good?

眉间皱痕 提交于 2019-12-05 13:25:21

问题


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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/1843459/javascript-the-good-parts-why-is-lookahead-not-good

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!