Skip to content

noMisleadingCharacterClass

诊断类别:lint/suspicious/noMisleadingCharacterClass

¥Diagnostic Category: lint/suspicious/noMisleadingCharacterClass

自从:v1.5.0

¥Since: v1.5.0

来源:

¥Sources:

禁止在字符类语法中使用多个代码点构成的字符。

¥Disallow characters made with multiple code points in character class syntax.

Unicode 包括由多个代码点组成的字符。例如 Á、🇯🇵、👨‍👩‍👦。RegExp 字符类 /[abc]/ 无法处理具有多个代码点的字符。例如,字符 ❇️ 由两个代码点组成: (U+2747) 和 VARIATION SELECTOR-16 (U+FE0F)。如果此字符属于 RegExp 字符类,它将匹配 VARIATION SELECTOR-16,而不是 ❇️。此规则报告字符类语法中包含多个代码点字符的正则表达式。

¥Unicode includes the characters which are made with multiple code points. e.g. Á, 🇯🇵, 👨‍👩‍👦. A RegExp character class /[abc]/ cannot handle characters with multiple code points. For example, the character ❇️ consists of two code points: (U+2747) and VARIATION SELECTOR-16 (U+FE0F). If this character is in a RegExp character class, it will match to either or VARIATION SELECTOR-16 rather than ❇️. This rule reports the regular expressions which include multiple code point characters in character class syntax.

¥Examples

¥Invalid

/^[Á]$/u;
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected combined character in the character class.

> 1 │ /^[Á]$/u;
^^^^^^^^
2 │

/^[❇️]$/u;
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected combined character in the character class.

> 1 │ /^[❇️]$/u;
^^^^^^^^
2 │

/^[👶🏻]$/u;
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected modified Emoji in the character class.

> 1 │ /^[👶🏻]$/u;
^^^^^^^^^^^
2 │

/^[🇯🇵]$/u;
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Regional indicator symbol characters should not be used in the character class.

> 1 │ /^[🇯🇵]$/u;
^^^^^^^^^
2 │

/^[👨‍👩‍👦]$/u;
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected joined character sequence in character class.

> 1 │ /^[👨‍👩‍👦]$/u;
^^^^^^^^^^^^^
2 │

/^[👍]$/; // surrogate pair without u flag
code-block.js:1:1 lint/suspicious/noMisleadingCharacterClass  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected surrogate pair in character class. Use the ‘u’ flag.

> 1 │ /^[👍]$/; // surrogate pair without u flag
^^^^^^^^
2 │

Safe fix: Add unicode u flag to regex

1 │ /^[👍]$/u;·//·surrogate·pair·without·u·flag
+

¥Valid

/^[abc]$/;
/^[👍]$/u;
/^[\q{👶🏻}]$/v;

¥Related links