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.

const pattern = /\a/;
const equals = /\=/;

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

const pattern = /[\!]/;
const caret = /[a\^b]/;

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

const pattern = /[\-ab]/;
const trailing = /[ab\-]/;

Some characters must be escaped to be treated literally.

const dot = /\./;
const star = /\*/;
const bracket = /[\]]/;
const range = /[a\-b]/;

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.

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