regexEmptyCharacterClasses
Reports character classes that match no characters.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
Reports empty character classes in regular expressions.
An empty character class [] matches nothing and always fails, which is usually a mistake.
Examples
Section titled “Examples”Empty Character Class
Section titled “Empty Character Class”An empty character class matches nothing.
const pattern = /[]/;const pattern = /[a]/;Empty Class in Pattern
Section titled “Empty Class in Pattern”An empty character class in a larger pattern causes the entire match to fail.
const pattern = /a[]b/;const pattern = /ab/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("[]");const pattern = new RegExp("[a-z]");Negated Empty Class Is Valid
Section titled “Negated Empty Class Is Valid”A negated empty character class [^] matches any character and is valid.
const pattern = /[^]/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally use empty character classes as a way to create a pattern that never matches (e.g., for testing purposes), you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.