Skip to content

regexStandaloneBackslashes

Reports standalone backslashes in regex patterns that look like incomplete escape sequences.

✅ This rule is included in the ts logical and logicalStrict presets.

In non-unicode mode, backslashes followed by certain characters are interpreted as literal backslash characters rather than escape sequences.

For example, /\c/ (without the unicode flag) is equivalent to /\\c/ — a literal backslash followed by c. This behavior is described in Annex B of the ECMAScript specification.

Reports standalone backslashes (\) in regex patterns that don’t form a valid escape sequence.

The \c escape requires a letter A-Z to form a valid control escape sequence.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\csp 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">61B<0\cX spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3

Using \c with a non-letter character results in a standalone backslash.

AEconsB0patteAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#F78C6C;--1:#AA09">82<\csp ansty="le--0:#5CA7E4;--1:#3A68">81<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\cA spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3

Standalone backslashes can also occur inside character classes.

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<\csp 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[<\cA] spans="tyle--0:#D9F5DD;--1:#11">1<111/ spans="tyle--0:#D6DEEB;--1:#40">3

The rule also checks regex patterns in RegExp constructor calls.

AEconsB0patteAE <= sp ansty="le--0:#7FDBCA;--1:#096E">72nAB0RegE5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#F78C6C;--1:#AA09">82<\\sp ansty="le--0:#ECC48D;--1:#984E">41<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEco61B0pat44A6E7<2new spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#82AAFF;--1:#3B">61B0Re31<111" spans="tyle--0:#F78C6C;--1:#AA">09<82\\ spans="tyle--0:#ECC48D;--1:#98">4E<4DcX spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">

Standard escape sequences like \n, \t, \d, and \\ are valid and not reported.

AEconsB0whitespaAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0\n\t<\rsp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0digiAE< =sp ansty="le--0:#5CA7E4;--1:#3A68">81<1/sp ansty="le--0:#82AAFF;--1:#3B61">B0<\dsp ansty="le--0:#7FDBCA;--1:#096E">7<2+sp ansty="le--0:#D9F5DD;--1:#1111">1<1/sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEconsB0literalBackslaAE< =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">

This rule is not configurable.

If your codebase intentionally uses standalone backslashes in regex patterns and you understand the Annex B behavior, you might prefer to disable this rule.