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.
Examples
Section titled “Examples”Optional Capturing Group
Section titled “Optional Capturing Group”const pattern = /(a)?b\1/;const pattern = /(a)b\1/;Zero-or-More Quantifier
Section titled “Zero-or-More Quantifier”const pattern = /(a)*\1/;const pattern = /(a)+\1/;Alternation Without Group
Section titled “Alternation Without Group”const pattern = /(?:(a)|b)\1/;const pattern = /(a)\1/;Group Reset in Loop
Section titled “Group Reset in Loop”const pattern = /(?:(a)|b)+\1/;const pattern = /(a)+\1/;Lookahead Ensures Match
Section titled “Lookahead Ensures Match”const pattern = /(a)?b\1/;const pattern = /(?=(a))\1b/;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 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.