Skip to content

regexCharacterClassSetOperations

Reports lookarounds that can be replaced with character class set operations.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Regular expressions with the v flag have access to character class set operations like intersection (&&) and subtraction (--). These are more readable and performant than using lookarounds to achieve the same effect.

76// Negative lookahead can be subtractiAEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(B0<\dsp ansty="le--0:#5CA7E4;--1:#3A68">8B0<\wsp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">85<3;d ivclassNa="meco">76// Positive lookbehind can be intersectiAEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0<\wsp ansty="le--0:#5CA7E4;--1:#3A68">8F(?<<=sp ansty="le--0:#82AAFF;--1:#3B61">B0<\dsp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">85<3;d ivclassNa="meco">76// Positive lookahead can be intersectiAEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#5CA7E4;--1:#3A68">8F(B0<\dsp ansty="le--0:#5CA7E4;--1:#3A68">8B0<\wsp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#5CA7E4;--1:#3A68">85<3;d ivclassNa="meco">
6376// Use subtraction instead of negative looka44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">6<1B0[ spans="tyle--0:#F78C6C;--1:#AA">09<82\w spans="tyle--0:#82AAFF;--1:#3B">61B0-<-\d] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fv spans="tyle--0:#D6DEEB;--1:#40">3<]/v><;di vdivclass="Nameexpressive-">6376// Use intersection instead of positive lookbe44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[\w&<&\d] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fv spans="tyle--0:#D6DEEB;--1:#40">3<]/v><;di vdivclass="Nameexpressive-">6376// Use intersection instead of positive looka44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[\w&<&\d] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fv spans="tyle--0:#D6DEEB;--1:#40">3<]/v><;di vdivclass="Nameexpressive-">6376// Lookarounds without the v flag are v44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688616<88F) spans="tyle--0:#82AAFF;--1:#3B">611<111/ spans="tyle--0:#D6DEEB;--1:#40">3<\w/><;di vdivclass="Nameexpressive-">6376// Complex lookarounds cannot be conve44AEco61B0pat446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">688F(?!611<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88Fv spans="tyle--0:#D6DEEB;--1:#40">3

This rule is not configurable.

If you prefer the explicit semantics of lookarounds over set operations, this rule might not be for you.