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

SyntaxError: invalid property name in regular expression

· 52CE 我爱测

The JavaScript exception "invalid property name in regular expression" or "invalid class property name in regular expression" occurs when the `\p` and `\P` Unicode character class escapes are not followed by a valid Unicode property name and/or value.

The JavaScript exception "invalid property name in regular expression" or "invalid class property name in regular expression" occurs when the \p and \P Unicode character class escapes are not followed by a valid Unicode property name and/or value.

Message

SyntaxError: Invalid regular expression: /\p{x}/u: Invalid property name (V8-based)
SyntaxError: Invalid regular expression: /[\p{x}]/u: Invalid property name in character class (V8-based)
SyntaxError: invalid property name in regular expression (Firefox)
SyntaxError: invalid class property name in regular expression (Firefox)
SyntaxError: Invalid regular expression: invalid property expression (Safari)

Error type

SyntaxError

What went wrong?

In Unicode-aware mode, the \p and \P escape sequences are used to match characters or character sequences based on their Unicode properties. The \p escape sequence matches characters with the specified Unicode property, while the \P escape sequence matches characters without the specified Unicode property. The syntax is:

\p{loneProperty}
\P{loneProperty}

\p{property=value}
\P{property=value}

If it's not followed by {, then a recognizable property name/value, then }, this syntax error is thrown. For more information on what lone properties and property names are allowed, see the Unicode character class escape reference.

Examples

Invalid cases

/\p{x}/u; // "x" is not a valid Unicode property name
/\p{Script=x}/u; // "x" is not a valid value for Script
/\property/u; // "\p" is not followed by the right syntax
/\p{RGI_Emoji_Flag_Sequence}/u;
// "RGI_Emoji_Flag_Sequence" is a property of strings, so the "v" flag must be used
/\P{RGI_Emoji_Flag_Sequence}/v; // \P cannot match properties of strings

Valid cases

/\p{Script=Latin}/u; // "Script=Latin" is a valid Unicode property
/\p{Letter}/u; // "Letter" is valid value for General_Category
/\p{RGI_Emoji_Flag_Sequence}/v; // Property of strings can only be used in "v" mode

See also

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

资料来源与适用范围

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

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

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