Aller au contenu

noMisleadingCharacterClass

Ce contenu n’est pas encore disponible dans votre langue.

Diagnostic Category: lint/suspicious/noMisleadingCharacterClass

Since: v1.5.0

Sources:

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.

/^[Á]$/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 flag
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
+
/^[abc]$/;
/^[👍]$/u;
/^[\q{👶🏻}]$/v;