no-misleading-character-class

Disallow characters made of several code points inside a regular expression character class

error by defaulthas suggestions

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:

bunfig.toml
[lint.rules]
"no-misleading-character-class" = "off" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-misleading-character-class" = ["error", {
  allowEscape = false,
}]

Examples#

example.js1 problem
const isThumbsUp = /^[👍]$/;
example.js3 problems
const isFlag = /^[🇯🇵🇩🇪]$/u;
example.js1 problem
const isFamily = new RegExp("[👨‍👩‍👦]", "u");
example.jsNo problems
const isThumbsUp = /^[👍]$/u;
example.jsNo problems
const isFlag = /^(?:🇯🇵|🇩🇪)$/u;

Options#

OptionDefaultWhat it does
allowEscapefalseDon't report code points that are written as escapes, such as \u0301. The escape shows that you meant a separate code point.
example.js1 problem
const accents = /[A\u0301]/;
example.jswith { "allowEscape": true }No problems
const accents = /[A\u0301]/;