JavaScript的正则表达式表示法有什么问题?

时间:2021-09-02 00:30:38

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?

我正在阅读Douglas Crockford的网页,JavaScript:世界上最容易被误解的编程语言,我不禁注意到,在设计错误下,他提到了“字面正则表达式的符号”。究竟他在说什么? JavaScript的正则表达式表示法有什么问题,为什么?

4 个解决方案

#1


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.

/ test //无效,而/ test \ //是有效的正则表达式。

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

在某些语言中,您实际上可以在字符串中指定标记字符,例如:

$regex = '#test/#';

Where # symbols do the denotion.

其中#符号表示。

#2


1  

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.

就个人而言,我发现正则表达式文字相当有用。它比新的RegExp(模式,标志)替代要简单得多,它需要遵守正则表达式转义和字符串转义规则(“Path \\\\ with \\\\ backslashes”,任何人?)。除了处理动态正则表达式之外,我无法看到这种表示法的巨大好处。

#3


0  

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.

他真的不清楚分号插入是错误的意思。也许他将分号视为语句分隔符。如果是这样的话,我不同意。没有分号代码混淆器/缩小器不会在您的代码上运行。

#4


0  

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.

可能搞乱了用于评论和划分的斜杠,或者因为“它们应该是一行,没有空格或插入评论”。

#1


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.

/ test //无效,而/ test \ //是有效的正则表达式。

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

在某些语言中,您实际上可以在字符串中指定标记字符,例如:

$regex = '#test/#';

Where # symbols do the denotion.

其中#符号表示。

#2


1  

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.

就个人而言,我发现正则表达式文字相当有用。它比新的RegExp(模式,标志)替代要简单得多,它需要遵守正则表达式转义和字符串转义规则(“Path \\\\ with \\\\ backslashes”,任何人?)。除了处理动态正则表达式之外,我无法看到这种表示法的巨大好处。

#3


0  

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.

他真的不清楚分号插入是错误的意思。也许他将分号视为语句分隔符。如果是这样的话,我不同意。没有分号代码混淆器/缩小器不会在您的代码上运行。

#4


0  

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.

可能搞乱了用于评论和划分的斜杠,或者因为“它们应该是一行,没有空格或插入评论”。