JavaScript 的正则表达式表示法有什么问题?
我正在阅读 Douglas Crockford 的网页,JavaScript:世界上最容易被误解的编程语言 ,我不禁注意到,在设计错误下,他提到了“文字正则表达式的符号”。他到底在说什么? JavaScript 的正则表达式表示法有什么问题,为什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能与它强制您转义
/
字符有关,也许他想要一个更独特的字符用作表示法。/test//
无效,而/test\//
是有效的正则表达式。而在某些语言中,您实际上可以在字符串中指定表示字符,例如:
其中
#
符号执行表示。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:
Where
#
symbols do the denotion.我可以想象,正则表达式文字符号是与语言规范分离的正则表达式引擎发展的障碍。
如果所有正则表达式都是字符串,那么它们在语言级别始终有效,并且正则表达式引擎可以更自由地解释它们。
但这只是一个猜测。我不知道克罗克福德的言论是什么意思。
就我个人而言,我发现正则表达式文字相当有用。它们比
new RegExp(pattern, flags)
替代方案要简洁得多,因为它需要遵守正则表达式转义和字符串转义规则(“Path\ \\\with\\\\反斜杠”
,有人吗?)。除了处理动态正则表达式之外,我看不出这种表示法有什么巨大的好处。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.
可能会因为 this 的注释和分割所使用的斜杠而混乱,或者因为“它们应该是一行,中间不能插入空格或注释”根据 this 。
Possibly messed up with slashes used for comments and division per this or because "they should be one line with no white space or commentary inserted to it" per this.