no-empty-character-class
Disallow empty character classes [] in regular expressions
warn by default
[] in a regular expression is a character class with no characters in it. It matches nothing, so /^#[]{6}$/ never matches any string. Most often someone forgot to fill in the class, or meant the text [], which is written \[].
[^], which matches any character, is allowed. Only regular expression literals are checked, not strings passed to new RegExp().
This rule is on by default.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"no-empty-character-class" = "off" # or "error"Examples#
example.js1 problem
const isHexColor = /^#[]{6}$/Empty class..test(color);example.jsNo problems
const isHexColor = /^#[0-9a-f]{6}$/i.test(color);example.jsNo problems
const anyCharacter = /[^]/;
const emptyArrayText = /\[]/;
const notChecked = new RegExp("[]");