0

是否在任何地方维护了所有可能的 JavaScript RegEx 语法冲突错误消息的完整列表?

try {
    new RegExp(pattern);
} catch (e) {
    console.log(e.message);
}

例如,一些可能的错误消息是:

Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
Uncaught SyntaxError: Invalid regular expression: /\/: \ at end of pattern
4

2 回答 2

3

Chrome V8 引擎源码regexp-parser.cc

"Regular expression too large"
"Unterminated group"
"Unmatched ')'"
"Nothing to repeat"
"Invalid group"
"Too many captures"
"\\ at end of pattern"
"Invalid property name"
"Invalid escape"
"Invalid decimal escape"
"Invalid unicode escape"
"Lone quantifier brackets"
"numbers out of order in {} quantifier"
"Incomplete quantifier"
"Invalid Unicode escape sequence"
"Invalid capture group name"
"Duplicate capture group name"
"Invalid named reference"
"Invalid named capture referenced"
"Invalid class escape"
"Invalid property name in character class"

21 条不同的错误消息,最长为 40 个字符。


Mozilla SpiderMonkey 引擎源代码js.msg

"back reference out of range in regular expression"
"invalid range in character class"
"\\ at end of pattern"
"RegExp exec method should return object or null"
"invalid decimal escape in regular expression"
"invalid regexp group"
"invalid identity escape in regular expression"
"invalid unicode escape in regular expression"
"unterminated parenthetical"
"can't supply flags when constructing one RegExp from another"
"nothing to repeat"
"numbers out of order in {} quantifier."
"character class escape cannot be used in class range in regular expression"
"raw brace is not allowed in regular expression with unicode flag"
"raw bracket is not allowed in regular expression with unicode flag"
"too many parentheses in regular expression"
"Unicode codepoint must not be greater than 0x10FFFF in {0}"
"unmatched ) in regular expression"
"unterminated character class"

19 条不同的错误消息,最长的是 74 个字符。

于 2017-06-15T14:17:46.123 回答
1

这取决于javascript引擎。

例如对于 v8 引擎浏览器检查正则表达式解析器源

搜索此模式ReportError(CStrVector(以查找所有错误。

以及解析器中作为指针引用的消息。

于 2017-06-15T14:14:22.183 回答