regexAmbiguousInvalidity
Reports regex patterns that use ambiguous or invalid syntax from Annex B.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
ECMAScript Annex B defines legacy regex syntax that browsers must support for web compatibility but is considered ambiguous or deprecated. This includes octal escapes in patterns, incomplete escape sequences, useless escapes, and unescaped special characters outside of character classes. Using strict regex syntax ensures better clarity and cross-platform compatibility.
When the u (unicode) or v (unicode sets) flag is present, JavaScript already enforces strict regex parsing, so this rule skips those patterns.
Examples
Section titled “Examples”// Octal escapes in regex patternsconst re1 = /\1/;const re2 = /\07/;
// Incomplete escape sequencesconst re3 = /\x1/;const re4 = /\u123/;const re5 = /\c1/;
// Unescaped brackets outside character classesconst re6 = /a]/;const re7 = /a{/;
// Useless escapesconst re8 = /\q/;const re9 = /\!/;
// Same issues with RegExp constructorconst re10 = new RegExp("\\1");// Use hexadecimal escapes instead of octalconst re1 = /\x01/;const re2 = /\x07/;
// Complete escape sequencesconst re3 = /\x1F/;const re4 = /\u0123/;const re5 = /\cA/;
// Escape brackets outside character classesconst re6 = /a\]/;const re7 = /a\{/;
// No useless escapes neededconst re8 = /q/;const re9 = /!/;
// Unicode mode enforces strict parsingconst re10 = /\1/u;const re11 = new RegExp("\\1", "u");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you are intentionally using legacy regex syntax for compatibility with very old JavaScript environments or specific regex behavior, you may choose to disable this rule. However, in modern development, using strict regex syntax is recommended.
Further Reading
Section titled “Further Reading”- ECMAScript Annex B: Additional ECMAScript Features for Web Browsers
- MDN: Regular expressions
- MDN: Unicode flag
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/strict