17

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.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
jilles de wit
  • 7,060
  • 3
  • 26
  • 50
  • 1
    The moment I read that sentence I googled it, and came up with this. Very strange - everything else he says makes perfect sense, and he almost always explains his reasons for saying things are "bad". – Skilldrick May 21 '10 at 13:59
  • 1
    I agree with @Skilldrick. Crockford has been really good at explaining his reasoning for nearly every point he makes in this book, but in this case he doesn't explain anything at all. I too googled after finding no explanation and ended up here. It seems to me like positive/negative lookaheads are awesome as long as you understand how they work and the potentially negative impact they could have on performance if used inappropriately. – CatDadCode Oct 22 '14 at 01:35

3 Answers3

10

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • 9
    I don't know what mr Crockford was thinking when he wrote this, but this seems the best reason to be careful with lookahead in javascript. It feels a bit unfair to blame the language for buggy implementations though. – jilles de wit Dec 17 '09 at 10:21
  • 6
    Remember: JavaScript can be used outside web, as node.js proves! – Eduardo Costa Nov 07 '11 at 20:48
  • 1
    Certainly! Remember though, that Javascript: the good parts was written before node.js came along, so this still qualifies as the best answer. – jilles de wit Feb 01 '14 at 21:47
3

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

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
  • 3
    I was thinking in that direction too, but lack of support in general doesn't really qualify it as a bad part of a specific language that actually supports it. – jilles de wit Dec 03 '09 at 22:57
2

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.

Richard Simões
  • 12,401
  • 6
  • 41
  • 50
  • 1
    too hard seems unlikely... performance might be a reason, but that is more of an interpreter issue that could be solved than a language specification issue. – jilles de wit Dec 03 '09 at 23:00
  • 3
    I wasn't really being serious with the "too hard" bit. But interpreter issues seem like alright reasons to say a language feature ought to be avoided. But, again, I'd disagree with Crockford that there is any kind of issue that would make lookaheads worth avoiding. Lookarounds are fantastic. – Richard Simões Dec 03 '09 at 23:14
  • 4
    Crockford's a smart guy, and I figure he would know better than anyone why he concluded lookaheads are bad, so I shot him an email. If he replies, I will post it here (if he doesn't do so himself). – Richard Simões Dec 03 '09 at 23:28
  • 1
    no answer yet I gather? I've tried to contact mr. Crockford as well. – jilles de wit Dec 07 '09 at 12:40