noMisleadingCharacterClass
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/suspicious/noMisleadingCharacterClass
Since: v1.5.0
Sources:
- Same as:
no-misleading-character-class
Disallow characters made with multiple code points in character class syntax.
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
Section titled ExamplesInvalid
Section titled Invalidcode-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.
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.
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.
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.
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.
code-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
│ +