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#
const isThumbsUp = /^[👍Unexpected surrogate pair in character class. Use u flag.]$/;const isFlag = /^[🇯Unexpected national flag in character class.🇵Unexpected national flag in character class.🇩Unexpected national flag in character class.🇪Unexpected national flag in character class.]$/u;const isFamily = new RegExp("[👨👩👦Unexpected joined character sequence in character class.]", "u");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\u0301Unexpected combined character in character class.]/;{ "allowEscape": true }No problemsconst accents = /[A\u0301]/;