Linter · Rules
no-misleading-character-class
Disallow characters made of several code points inside a regular expression character class
A character class such as [ab] matches one code point from the list. Many things that look like one character are several: a letter followed by a combining accent, an emoji with a skin tone, a flag, a family joined with zero-width joiners. Without the u flag, most emoji count as two. Inside [...] each part becomes its own alternative, so /^[👍]$/.test("👍") is false.
The rule checks regular expression literals and the patterns given to RegExp(...) and new RegExp(...).
--fix-suggestions adds the u flag when that's all that's missing and the pattern stays valid with it.
This rule is on by default.
Configure
On by default as an error. To turn it off:
[lint.rules]
"no-misleading-character-class" = "off" # or "warn"
Every option, set to its default:
[lint.rules]
"no-misleading-character-class" = ["error", {
allowEscape = false,
}]
Examples
Reported:
const isThumbsUp = /^[👍]$/;
const isFlag = /^[🇯🇵🇩🇪]$/u;
const isFamily = new RegExp("[👨👩👦]", "u");
Not reported:
const isThumbsUp = /^[👍]$/u;
const isFlag = /^(?:🇯🇵|🇩🇪)$/u;
Options
| Option | Default | What it does |
|---|---|---|
allowEscape | false | Don't report code points that are written as escapes, such as \u0301. The escape shows that you meant a separate code point. |
const accents = /[A\u0301]/;
const accents = /[A\u0301]/;