RegExp.prototype.unicode

RegExp 实例的 unicode 访问器属性返回此正则表达式是否使用 u 标志。

¥The unicode accessor property of RegExp instances returns whether or not the u flag is used with this regular expression.

Try it

描述

¥Description

如果使用了 u 标志,则 RegExp.prototype.unicode 的值为 true;否则,falseu 标志启用各种与 Unicode 相关的功能。带有 "u" 标志:

¥RegExp.prototype.unicode has the value true if the u flag was used; otherwise, false. The u flag enables various Unicode-related features. With the "u" flag:

  • 任何 Unicode 代码点转义 (\u{xxxx}, \p{UnicodePropertyValue}) 都将被解释为这样,而不是身份转义。例如,/\u{61}/u 匹配 "a",但 /\u{61}/(不带 u 标志)匹配 "u".repeat(61),其中 \u 相当于单个 u
  • 代理对将被解释为整个字符而不是两个单独的字符。例如,/[😄]/u 只能匹配 "😄",但不能匹配 "\ud83d"
  • lastIndex 自动前进时(例如调用 exec() 时),unicode 正则表达式按 Unicode 代码点而不是 UTF-16 代码单元前进。

解析行为还有其他一些更改可以防止可能的语法错误(类似于正则表达式语法的 严格模式)。这些语法都是 已弃用,仅为网络兼容性而保留,你不应该依赖它们。

¥There are other changes to the parsing behavior that prevent possible syntax mistakes (which are analogous to strict mode for regex syntax). These syntaxes are all deprecated and only kept for web compatibility, and you should not rely on them.

unicode 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of unicode is undefined. You cannot change this property directly.

Unicode 识别模式

¥Unicode-aware mode

当我们提到 Unicode 感知模式时,我们的意思是正则表达式具有 uv 标志,在这种情况下,正则表达式启用 Unicode 相关功能(例如 Unicode 字符类转义)并具有更严格的语法规则。由于 uv 以不兼容的方式解释相同的正则表达式,因此使用这两个标志会产生 SyntaxError

¥When we refer to Unicode-aware mode, we mean the regex has either the u or the v flag, in which case the regex enables Unicode-related features (such as Unicode character class escape) and has much stricter syntax rules. Because u and v interpret the same regex in incompatible ways, using both flags results in a SyntaxError.

同样,如果正则表达式既没有 u 也没有 v 标志,则它不支持 Unicode。在这种情况下,正则表达式被解释为 UTF-16 代码单元的序列,并且有许多遗留语法不会成为语法错误。

¥Similarly, a regex is Unicode-unaware if it has neither the u nor the v flag. In this case, the regex is interpreted as a sequence of UTF-16 code units, and there are many legacy syntaxes that do not become syntax errors.

示例

¥Examples

使用 unicode 属性

¥Using the unicode property

js
const regex = /\u{61}/u;

console.log(regex.unicode); // true

规范

Specification
ECMAScript Language Specification
# sec-get-regexp.prototype.unicode

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看