javascript - 为什么 `/\:/u` 会抛出 “invalid escape” 错误?

标签 javascript regex

<分区>

我有这样的代码:

url.match(/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/ui)

ESLint 说 Parsing error: Invalid regular expression:/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/: 无效转义

我不明白为什么这个正则表达式是错误的。我该如何解决?

最佳答案

不必要的转义序列对于 u 无效旗帜

\:是不必要的转义序列。使用 u 时,这些无效旗帜。只需使用 :相反。

规范、调试器、文档

这些是字符类之外的特殊字符的有效且必要的转义序列:\$ , \( , \) , \* , \+ , \. , \? , \[ , \\ , \] , \^ , \{ , \| , \} (所有 “syntax characters” )和 \/ (identity escape 的特例)。

其他转义序列,如 \ , \! , \" , \# , \% , \& , \' , \, , \- , \: , \; , \< , \= , \> , \@ , \_ , \` , \~是不必要的,因此对 u 无效旗帜。

查看 specification详细了解所有转义规则。1


类似 RegEx101 的工具报告这个 —— 虽然有点神秘:

/\:/u:

\: — This token has no special meaning and has thus been rendered erroneous


至于文档,我刚刚regex cheat sheet on MDN 中添加了注释:

Note that some characters like :, -, @, etc. neither have a special meaning when escaped nor when unescaped. Escape sequences like \:, \-, \@ will be equivalent to their literal, unescaped character equivalents in regular expressions. However, in regular expressions with the unicode flag, these will cause an invalid identity escape error.

理由

注释继续:

This is done to ensure backward compatibility with existing code that uses new escape sequences like \p or \k.

提出和引入该功能时,这就是 proposal’s FAQ 的内容不得不说:

What about backwards compatibility?

In regular expressions without the u flag, the pattern \p is an (unnecessary) escape sequence for p. Patterns of the form \p{Letter} might already be present in existing regular expressions without the u flag, and therefore we cannot assign new meaning to such patterns without breaking backwards compatibility.

For this reason, ECMAScript 2015 made unnecessary escape sequences like \p and \P throw an exception when the u flag is set. This enables us to change the meaning of \p{…} and \P{…} in regular expressions with the u flag without breaking backwards compatibility.

此页面也链接自此 ES Discuss thread提出这个问题的地方:

Why is RegExp /\-/u a syntax error?

JSLint previously warned against unescaped literal - in RegExp. However, escaping - together with unicode flag u causes a syntax error in Chrome, Firefox, and Edge (and JSLint has since removed the warning). Just curious about the reason why the above edge-case is a syntax error.

(Minor grammar adjustments by me.)

回复链接到上面的 GitHub 存储库和提案,但也以不同的方式解释了基本原理:

Think of the u flag as a strict mode for regular expressions.

因此,每当您使用 u标志,请记住这一点。 使用 u 后,正则表达式的行为开始有所不同. 某些新事物变得有效,但某些其他事物也变得无效。 例如,另见 Why is /[\w-+]/ a valid regex but /[\w-+]/u invalid? .


1:你会发现 [U] 的某些产生式规则这是表示 Unicode 模式的参数。 查看grammar notation reference用于解码这些。

关于javascript - 为什么 `/\:/u` 会抛出 “invalid escape” 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63006601/

相关文章:

javascript - 有没有办法清除所有innerHTML?

regex - 从两个特定字符串之间的任何位置删除特定字符?

regex - 在 OSX 和 GNU 中删除带有 "find"数字的文件名

正则表达式替换匹配项,但也会忽略引号内的匹配项

javascript - 正则表达式模式不按预期工作

javascript - 如何从内部更改 iframe z-index?

javascript - 如何使用 webpack 将脚本文件从 src 复制到 dist

python - python中模式匹配时间格式的正则表达式

javascript - 查找嵌套对象中的最小值和最大值

javascript - 如何在ag-grid中选择列