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.

const pattern = /\c/;

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

const pattern = /\c1/;

Standalone backslashes can also occur inside character classes.

const pattern = /[\c]/;

The rule also checks regex patterns in RegExp constructor calls.

const pattern = new RegExp("\\c");

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

const whitespace = /\n\t\r/;
const digits = /\d+/;
const literalBackslash = /\\/;

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.