regex - What's wrong with JavaScript's regular expression notation? - Stack Overflow

I was reading Douglas Crockford's web page, JavaScript: The World's Most Misunderstood Progra

I was reading Douglas Crockford's web page, JavaScript: The World's Most Misunderstood Programming Language, and I couldn't help but notice that, under Design Errors, he mentions "the notation for literal regular expressions." What exactly is he talking about? What's wrong with JavaScript's notation for regular expressions, and why?

I was reading Douglas Crockford's web page, JavaScript: The World's Most Misunderstood Programming Language, and I couldn't help but notice that, under Design Errors, he mentions "the notation for literal regular expressions." What exactly is he talking about? What's wrong with JavaScript's notation for regular expressions, and why?

Share Improve this question edited Jun 20, 2010 at 6:52 cherouvim 31.9k15 gold badges106 silver badges156 bronze badges asked Jun 20, 2010 at 6:35 Sasha ChedygovSasha Chedygov 131k26 gold badges106 silver badges116 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 9

Might have to do with the fact that it enforces you to escape / characters, perhaps he wanted a more unique character to use as the notation.

/test// is invalid, while /test\// is a valid regex.

Whereas in some languages you can actually specify the denotion character in a string, eg:

$regex = '#test/#';

Where # symbols do the denotion.

I could imagine that the regex literal notation is a hindrance for evolving the regex engine decoupled from the language specification.

If all regexes were strings, they were always valid at the language level, and the regex engine could interpret them more freely.

But that's just a guess. I have no idea what Crockford meant with his statement.

Personally I find regex literals rather helpful. The are a lot less verbose than the new RegExp(pattern, flags) alternative with its need to adhere to both regex escaping and string escaping rules ("Path\\\\with\\\\backslashes", anyone?). I can't see the huge benefit for this notation, other than for dealing with dynamic regexes.

He really isn't very clear on what he means by semicolon insertion being a mistake. Perhaps he means semicolons as statement delimiters. If that's the case, I disagree. Without semicolons code obfuscators/minifiers don't run on your code.

Possibly messed up with slashes used for ments and division per this or because "they should be one line with no white space or mentary inserted to it" per this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743745217a4499878.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信