Skip to content

regexUnnecessaryBackreferences

Reports backreferences in regular expressions that will always match empty or fail.

✅ This rule is included in the ts logical presets.

Reports backreferences in regular expressions that will always match empty or fail to match. These backreferences are useless because they reference capturing groups that haven’t captured anything at the time the backreference is evaluated.

A backreference inside its own capturing group will always match empty since the group hasn’t finished capturing yet.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(a\<1)sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<\1/>

A backreference that appears before the group it references will always match empty since the group hasn’t been matched yet.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F\1(1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<\1/>

A backreference in a different alternative from its group will always match empty since the group’s alternative wasn’t taken.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(7<2|sp ansty="le--0:#5CA7E4;--1:#3A68">8F<\1sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(66<88Fb spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<|b/>

A backreference to a group inside a negative lookaround will always match empty since the group wasn’t matched.

AEconsB0patteAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(?!(a<))sp ansty="le--0:#82AAFF;--1:#3B61">B0<\wsp ansty="le--0:#5CA7E4;--1:#3A68">8F<\1sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat44A1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(?=<(a)) spans="tyle--0:#82AAFF;--1:#3B">6168<8F\1 spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<\1/>

In lookbehind assertions, matching proceeds right-to-left, so a backreference after its group is a backward reference.

AEconsB0patteAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(?<=(a)\1<)bsp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">
44AEco61B0pat44A1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(?<=\1(1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<)b/>

This rule is not configurable.

If you are intentionally using backreferences that match empty strings for specific pattern-matching logic, you might want to disable this rule. However, in most cases, these patterns indicate a mistake in the regular expression.