eslint/no-misleading-character-class Correctness
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.,
AฬwhereAis 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:
/^[Aฬ]$/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");Configuration โ
This rule accepts a configuration object with the following properties:
allowEscape โ
type: boolean
default: false
When set to true, 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 using the config file or in the CLI, you can use:
{
"rules": {
"no-misleading-character-class": "error"
}
}oxlint --deny no-misleading-character-class