SyntaxError: nothing to repeat
当正则表达式中的量词应用于空内容或应用于断言时,就会发生 JavaScript 异常“nothing to repeat”或“invalid quantifier in regular expression”。
消息
SyntaxError: Invalid regular expression: /\b+/: Nothing to repeat (V8-based) SyntaxError: Invalid regular expression: /(?=)+/u: Invalid quantifier (V8-based) SyntaxError: nothing to repeat (Firefox) SyntaxError: invalid quantifier in regular expression (Firefox) SyntaxError: Invalid regular expression: nothing to repeat (Safari)
错误类型
哪里出错了?
示例
无效情况
js
/\b+/; // \b is a word boundary assertion, it doesn't consume characters
/(*hello*)/;
有效情况
js
/b+/; // b is a character, it can be repeated
/(\*hello\*)/; // Escape the asterisks to match them literally