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.
Examples
Section titled “Examples”Unnecessary Escape at Root
Section titled “Unnecessary Escape at Root”Characters that are not special regex metacharacters don’t need to be escaped.
const pattern = /\a/;const equals = /\=/;const pattern = /a/;const equals = /=/;Unnecessary Escape in Character Class
Section titled “Unnecessary Escape in Character Class”Inside a character class, most characters don’t need escaping.
const pattern = /[\!]/;const caret = /[a\^b]/;const pattern = /[!]/;const caret = /[a^b]/;Hyphen at Edge of Character Class
Section titled “Hyphen at Edge of Character Class”A hyphen at the start or end of a character class doesn’t need escaping.
const pattern = /[\-ab]/;const trailing = /[ab\-]/;const pattern = /[-ab]/;const trailing = /[ab-]/;Necessary Escapes
Section titled “Necessary Escapes”Some characters must be escaped to be treated literally.
const dot = /\./;const star = /\*/;const bracket = /[\]]/;const range = /[a\-b]/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer to escape characters for visual clarity even when not strictly necessary, you might want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-useless-escape - Oxlint:
eslint/no-useless-escape