noMisleadingCharacterClass
¥Summary
-
规则生效日期:
v1.5.0¥Rule available since:
v1.5.0 -
诊断类别:
lint/suspicious/noMisleadingCharacterClass¥Diagnostic Category:
lint/suspicious/noMisleadingCharacterClass -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-misleading-character-class相同¥Same as
no-misleading-character-class
-
¥How to configure
{ "linter": { "rules": { "suspicious": { "noMisleadingCharacterClass": "error" } } }}¥Description
禁止在字符类语法中使用多个代码点构成的字符。
¥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 expression literals which include multiple code point characters in character class syntax.
¥Examples
¥Invalid
/^[Á]$/u;code-block.js:1:4 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match a character and a combining character.
> 1 │ /^[Á]$/u;
│ ^
2 │
ℹ A character and a combining character forms a new character. Replace the character class with an alternation.
/^[❇️]$/u;code-block.js:1:4 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match a character and a combining character.
> 1 │ /^[❇️]$/u;
│ ^
2 │
ℹ A character and a combining character forms a new character. Replace the character class with an alternation.
/^[👶🏻]$/u;code-block.js:1:4 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match an emoji with a skin tone modifier.
> 1 │ /^[👶🏻]$/u;
│ ^^^^
2 │
ℹ Replace the character class with an alternation.
/^[🇯🇵]$/u;code-block.js:1:4 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match a pair of regional indicator symbols.
> 1 │ /^[🇯🇵]$/u;
│ ^^
2 │
ℹ A pair of regional indicator symbols encodes a country code. Replace the character class with an alternation.
/^[👨👩👦]$/u;code-block.js:1:4 lint/suspicious/noMisleadingCharacterClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match a joined character sequence.
> 1 │ /^[👨👩👦]$/u;
│ ^^^^
2 │
ℹ A zero width joiner composes several emojis into a new one. Replace the character class with an alternation.
/^[👍]$/; // surrogate pair without u flagcode-block.js:1:4 lint/suspicious/noMisleadingCharacterClass FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ A character class cannot match a surrogate pair. Add the ‘u’ unicode flag to match against them.
> 1 │ /^[👍]$/; // surrogate pair without u flag
│ ^^
2 │
ℹ A surrogate pair forms a single codepoint, but is encoded as a pair of two characters. Without the unicode flag, the regex matches a single surrogate character.
ℹ Safe fix: Add unicode u flag to regex
1 │ /^[👍]$/u;·//·surrogate·pair·without·u·flag
│ +
¥Valid
/^[abc]$/;/^[👍]$/u;/^[\q{👶🏻}]$/v;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号