regexStandaloneBackslashes
Reports standalone backslashes in regex patterns that look like incomplete escape sequences.
✅ This rule is included in the ts logical 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.
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 pattern = /\c/;const 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 pattern = /\c1/;const pattern = /\cA/;Inside Character Class
Section titled “Inside Character Class”Standalone backslashes can also occur inside character classes.
const pattern = /[\c]/;const pattern = /[\cA]/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("\\c");const pattern = new RegExp("\\cX");Valid Escape Sequences
Section titled “Valid Escape Sequences”Standard escape sequences like \n, \t, \d, and \\ are valid and not reported.
const whitespace = /\n\t\r/;const digits = /\d+/;const 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