Skip to content

regexUnnecessaryReferentialBackreferences

Reports backreferences that may reference a capturing group that was not matched.

✅ This rule is included in the ts logical presets.

It is possible to make a referenced group of a backreference not matched because some other path leads to the backreference. In that case the backreference will trivially accept (e.g. /(?:(a)|b)\1/). This can happen when the group is inside an optional quantifier, inside an alternation where some branches don’t include it, or when the group can be reset in a loop. Similarly, this can will happen if the captured text of the referenced group was reset before reaching the backreference.

This rule reports backreferences that may always be empty because the referenced capturing group might not participate in the match.

const pattern = /(a)?b\1/;
const pattern = /(a)*\1/;
const pattern = /(?:(a)|b)\1/;
const pattern = /(?:(a)|b)+\1/;
const pattern = /(a)?b\1/;

This rule is not configurable.

If you intentionally use backreferences that may be empty to match optional content conditionally, you may want to disable this rule. You might consider using Flint disable comments and/or configuration file disables for specific cases instead of completely disabling this rule.

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