Skip to content

regexUnnecessaryEscapes

Reports unnecessary escape sequences in regular expressions.

✅ This rule is included in the ts stylistic presets.

Backslashes (\) are only necessary in regular expressions to escape special characters. Using them on non-special characters does not change the regular expression. They are often at least visual noise and sometimes a sign of programmer error.

This rule reports escape sequences in regular expressions where the backslash is unnecessary.

Characters that are not special regex metacharacters don’t need to be escaped.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\asp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0equaAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\=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">6<88Fa spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0eq446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#5CA7E4;--1:#3A">6<88F= spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3

Inside a character class, most characters don’t need escaping.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B<0[sp ansty="le--0:#F78C6C;--1:#AA09">82<\!sp ansty="le--0:#82AAFF;--1:#3B61">B<0]sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0carAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0<[asp ansty="le--0:#F78C6C;--1:#AA09">82<\^sp ansty="le--0:#82AAFF;--1:#3B61">B01<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:#82AAFF;--1:#3B">61B<0[!] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0c446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[1<111/ spans="tyle--0:#D6DEEB;--1:#40">3

A hyphen at the start or end of a character class doesn’t need escaping.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0[\-a1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0trailiAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0[82<\-sp ansty="le--0:#82AAFF;--1:#3B61">B<0]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:#82AAFF;--1:#3B">61B0[<-ab] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">344AEco61B0trai446<88F spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#82AAFF;--1:#3B">61B0[1<111/ spans="tyle--0:#D6DEEB;--1:#40">3<-]/>

Some characters must be escaped to be treated literally.

AEconsB0dAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\.sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0stAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\*sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0brackAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B<0[sp ansty="le--0:#F78C6C;--1:#AA09">82<\]sp ansty="le--0:#82AAFF;--1:#3B61">B<0]sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0ranAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0[a\-1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco">

This rule is not configurable.

If you prefer to escape characters for visual clarity even when not strictly necessary, you might want to disable this rule.