大网数据广告活动
Web 标准与前端开发

SyntaxError: nothing to repeat

· 52CE 我爱测

The JavaScript exception "nothing to repeat" or "invalid quantifier in regular expression" occurs when a quantifier in a regular expression is applied to nothing or applied to an assertion.

The JavaScript exception "nothing to repeat" or "invalid quantifier in regular expression" occurs when a quantifier in a regular expression is applied to nothing or applied to an assertion.

Message

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)

Error type

SyntaxError

What went wrong?

Quantifiers are used to specify how many times a character or group of characters can appear in a regular expression. For example, a{3} matches the character a exactly three times. Therefore, if the thing preceding the quantifier is not something that matches characters, the quantifier is invalid. For example: quantifiers at the start of a capturing group, at the start of a disjunction alternative, etc., cannot repeat anything. Assertions don't consume characters, so it also doesn't make sense to repeat them.

In Unicode-unaware mode, there's a deprecated syntax that allows the lookahead assertions to be quantified. This is a deprecated syntax and you should not rely on it.

Examples

Invalid cases

/\b+/; // \b is a word boundary assertion, it doesn't consume characters
/(*hello*)/;

Valid cases

/b+/; // b is a character, it can be repeated
/(\*hello\*)/; // Escape the asterisks to match them literally

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。