eslint/no-misleading-character-class Nursery
What it does
This rule reports regular expressions which include multiple code point characters in character class syntax. This includes:
- Characters with combining marks (e.g.,
Á
whereA
is followed by a combining acute accent) - Characters with emoji modifiers (e.g.,
👶🏻
) - Pairs of regional indicator symbols (e.g.,
🇯🇵
) - Characters joined by zero-width joiner (ZWJ) (e.g.,
👨👩👦
) - Surrogate pairs without the Unicode flag (e.g.,
/^[👍]$/
)
Why is this bad?
Unicode includes characters which are made by multiple code points. RegExp character class syntax (/[abc]/
) cannot handle characters which are made by multiple code points as a character; those characters will be dissolved to each code point. For example, ❇️
is made by ❇
(U+2747
) and VARIATION SELECTOR-16 (U+FE0F
). If this character is in a RegExp character class, it will match either ❇
(U+2747
) or VARIATION SELECTOR-16 (U+FE0F
) rather than ❇️
.
This can lead to regular expressions that do not match what the author intended, especially for emoji, regional indicators, and characters with combining marks.
Examples
Examples of incorrect code for this rule:
/^[Á]$/u;
/^[❇️]$/u;
/^[👶🏻]$/u;
/^[🇯🇵]$/u;
/^[👨👩👦]$/u;
/^[👍]$/;
new RegExp("[🎵]");
Examples of correct code for this rule:
/^[abc]$/;
/^[👍]$/u;
/[\u00B7\u0300-\u036F]/u;
new RegExp("^[\u{1F1EF}\u{1F1F5}]", "u");
Options
This rule has an object option:
allowEscape
: When set totrue
, the rule allows any grouping of code points inside a character class as long as they are written using escape sequences.
Examples of incorrect code for this rule with { "allowEscape": true }
:
/[\uD83D]/; // backslash can be omitted
new RegExp("[\ud83d" + "\udc4d]");
Examples of correct code for this rule with { "allowEscape": true }
:
/[\ud83d\udc4d]/;
/[\u00B7\u0300-\u036F]/u;
/[👨\u200d👩]/u;
new RegExp("[\x41\u0301]");
new RegExp(`[\u{1F1EF}\u{1F1F5}]`, "u");
new RegExp("[\\u{1F1EF}\\u{1F1F5}]", "u");
How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny no-misleading-character-class
{
"rules": {
"no-misleading-character-class": "error"
}
}