Skip to content

regexEmptyStringLiterals

Reports empty string literals in character classes.

✅ This rule is included in the ts logical presets.

Reports empty string literals (\q{}) in character classes when using the v (unicodeSets) flag. An empty string literal in a character class always matches the empty string, which is usually a mistake.

A \q{} with no content matches the empty string.

const pattern = /[\q{}]/v;

Even with other elements in the character class, an empty \q{} is reported.

const pattern = /[a\q{}]/v;

The rule also checks regex patterns in RegExp constructor calls.

const pattern = new RegExp("[\\q{}]", "v");

If the string literal has at least one non-empty alternative, it is valid.

const pattern = /[\q{a|}]/v;

This rule is not configurable.

If you intentionally use empty string literals in character classes to match empty strings and prefer this style over using quantifiers, you might prefer to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.