regexStandaloneBackslashes
Reports standalone backslashes in regex patterns that look like incomplete escape sequences.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
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.
Examples
Section titled “Examples”Incomplete Control Escape
Section titled “Incomplete Control Escape”The \c escape requires a letter A-Z to form a valid control escape sequence.
const const pattern: RegExp
pattern = /\c/;const const pattern: RegExp
pattern = /\cX/;Invalid Control Escape Character
Section titled “Invalid Control Escape Character”Using \c with a non-letter character results in a standalone backslash.
const const pattern: RegExp
pattern = /\c1/;const const pattern: RegExp
pattern = /\cA/;Inside Character Class
Section titled “Inside Character Class”Standalone backslashes can also occur inside character classes.
const const pattern: RegExp
pattern = /[\c]/;const const pattern: RegExp
pattern = /[\cA]/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("\\c");const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("\\cX");Valid Escape Sequences
Section titled “Valid Escape Sequences”Standard escape sequences like \n, \t, \d, and \\ are valid and not reported.
const const whitespace: RegExp
whitespace = /\n\t\r/;const const digits: RegExp
digits = /\d+/;const const literalBackslash: RegExp
literalBackslash = /\\/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase intentionally uses standalone backslashes in regex patterns and you understand the Annex B behavior, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-standalone-backslash