regexDollarEscapes
Reports replacement strings with unescaped
$that should use$$.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
Enforces escaping the $ character as $$ when using it literally in the replacement argument of String.prototype.replace() and String.prototype.replaceAll().
In these methods, $ has special meaning for substitution patterns like $& (matched substring), $1 (first capture group), etc.
An unescaped $ followed by an unrecognized character will be treated as a literal $, but this behavior is confusing and error-prone.
This rule reports unescaped $ characters in replacement strings that are not valid substitution pattern matchers.
Examples
Section titled “Examples”Unescaped Dollar Sign
Section titled “Unescaped Dollar Sign”const result = "€100".replace(/€/, "$");const result = "€100".replace(/€/, "$$");Dollar Before Invalid Substitution Character
Section titled “Dollar Before Invalid Substitution Character”const result = "abc".replace(/./, "$x");const result = "abc".replace(/./, "$$x");Valid Substitution Patterns
Section titled “Valid Substitution Patterns”const matchedSubstring = "abc".replace(/./, "$&");const firstGroup = "abc".replace(/(.)/, "$1");const namedGroup = "abc".replace(/(?<char>.)/, "$<char>");const literalDollar = "abc".replace(/./, "$$");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally rely on the behavior where $ followed by an unrecognized character produces a literal $, you might prefer to disable this rule.