require-unicode-regexp

Require the u or v flag on regular expressions

has suggestions

Without the u or v flag, a regular expression works on 16-bit units, not on characters. /^.$/ doesn't match a single emoji such as "\u{1F600}", because it takes two units. The flags also make the pattern strict, so a useless escape such as \a is a syntax error instead of being silently read as a.

The rule checks regular expression literals and calls to RegExp, with or without new. If the flags aren't a constant string, the call isn't reported.

The report comes with a suggestion that adds the flag, if the pattern is still valid with it.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"require-unicode-regexp" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"require-unicode-regexp" = ["error", {
  # requireFlag = <"u" or "v">,
}]

Examples#

example.js2 problems
const oneCharacter = /^.$/;
const word = new RegExp("\\w+", "g");
example.jsNo problems
const oneCharacter = /^.$/u;
const word = new RegExp("\\w+", "gu");
const emoji = /^\p{RGI_Emoji}$/v;

function search(pattern, flags) {
  return new RegExp(pattern, flags);
}

Options#

OptionDefaultWhat it does
requireFlagnot setSet it to "u" or "v" to accept only that flag. When it isn't set, either flag is accepted.

With requireFlag set to "v", the u flag is reported:

example.jswith { "requireFlag": "v" }1 problem
const oneCharacter = /^.$/u;