regexOctalEscapes
Reports octal escape sequences in regular expressions.
✅ This rule is included in the ts logical presets.
Reports octal escape sequences in regular expressions.
Octal escapes like \7 or \07 can be confused with backreferences.
The same sequence (e.g., \2) may be a character or a backreference depending on the number of capturing groups in the pattern.
Examples
Section titled “Examples”Octal Escape Starting with Zero
Section titled “Octal Escape Starting with Zero”const pattern = /\07/;const pattern = /\x07/;Simple Octal Escape
Section titled “Simple Octal Escape”const pattern = /\7/;const pattern = /\x07/;Ambiguous Octal vs Backreference
Section titled “Ambiguous Octal vs Backreference”const pattern = /\1\2/;const pattern = /\x01\x02/;In the incorrect example, \1 and \2 are octal escapes (matching characters with code 1 and 2).
If capturing groups are added later, they could become backreferences.
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally use octal escapes for specific character matching and understand the distinction from backreferences, you might want to disable this rule.
Octal escapes inside character class ranges (e.g., [\1-\4]) are allowed.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-octal
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.