Skip to content

noMisleadingCharacterClass

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