A regular expression has an empty alternative or an empty capturing group

tx0/ts:regex-empty-branchrecommendedwarningbug

What it finds

/jpg|png|/, /(|-)\d+/, /a()b/: an empty alternative matches the empty string, so the alternation as a whole always matches, and an empty group matches nothing useful. Usually a | too many was typed, or something was deleted from the group.

An empty alternative is reported in a pattern or group with more than one alternative; (?:) alone, sometimes written to match nothing on purpose, is reported as an empty group only when it captures.

From the rule's source, regex_empty_branch.rs.

Example from the rule's tests
const a = /jpg|png|/;tx0 reports: this pattern has an empty alternativeconst b = /(|-)\d+/;tx0 reports: a group in this pattern has an empty alternativeconst c = /a()b/;tx0 reports: this pattern has an empty group ()const d = /(?:a|b)?c/;const e = /a|b/;
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:regex-empty-branch" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:regex-empty-branch] reason="..."

tx0 explain tx0/ts:regex-empty-branch shows the same in the terminal. Found a false positive? Report it.