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
const pattern: RegExp
pattern
= /\a/;
const
const equals: RegExp
equals
= /\=/;

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

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

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

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

Some characters must be escaped to be treated literally.

const
const dot: RegExp
dot
= /\./;
const
const star: RegExp
star
= /\*/;
const
const bracket: RegExp
bracket
= /[\]]/;
const
const range: RegExp
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 ❤️‍🔥 around the world by the Flint team and contributors.