Skip to content

regexAmbiguousInvalidity

Reports regex patterns that use ambiguous or invalid syntax from Annex B.

✅ This rule is included in the ts logical presets.

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.

// Octal escapes in regex patterns
const re1 = /\1/;
const re2 = /\07/;
// Incomplete escape sequences
const re3 = /\x1/;
const re4 = /\u123/;
const re5 = /\c1/;
// Unescaped brackets outside character classes
const re6 = /a]/;
const re7 = /a{/;
// Useless escapes
const re8 = /\q/;
const re9 = /\!/;
// Same issues with RegExp constructor
const re10 = new RegExp("\\1");

This rule is not configurable.

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.

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