Skip to content
โ† Back to rules

eslint/no-misleading-character-class Correctness

โœ… This rule is turned on by default.
๐Ÿšง An auto-fix is planned for this rule, but not implemented at this time.

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ฬ where A 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:

javascript
/^[Aฬ]$/u;
/^[โ‡๏ธ]$/u;
/^[๐Ÿ‘ถ๐Ÿป]$/u;
/^[๐Ÿ‡ฏ๐Ÿ‡ต]$/u;
/^[๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆ]$/u;
/^[๐Ÿ‘]$/;
new RegExp("[๐ŸŽต]");

Examples of correct code for this rule:

javascript
/^[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 }:

javascript
/[\uD83D]/; // backslash can be omitted
new RegExp("[\ud83d" + "\udc4d]");

Examples of correct code for this rule with { "allowEscape": true }:

javascript
/[\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:

json
{
  "rules": {
    "no-misleading-character-class": "error"
  }
}
bash
oxlint --deny no-misleading-character-class

References โ€‹