Skip to content

regexEmptyAlternatives

Reports empty alternatives in regular expressions that may indicate a mistake.

✅ This rule is included in the ts logical presets.

Empty alternatives in regular expressions match zero characters. While sometimes intentional, they often indicate a mistake such as a typo or incomplete pattern.

An empty alternative at the end of a group or pattern.

const pattern = /a|/;

An empty alternative at the start of a group or pattern.

const pattern = /|a/;

Empty alternatives in capturing and non-capturing groups.

const pattern = /(a|)/;

The rule also checks regex patterns in RegExp constructor calls.

const pattern = new RegExp("a|b|");

This rule is not configurable.

If you intentionally use empty alternatives to make a pattern optional and prefer this style over quantifiers, you might prefer to disable this rule. Some developers find (a|) more readable than a? for complex patterns.

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