Skip to content

regexDollarEscapes

Reports replacement strings with unescaped $ that should use $$.

✅ This rule is included in the ts stylistic presets.

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.

const result = "€100".replace(//, "$");

Dollar Before Invalid Substitution Character

Section titled “Dollar Before Invalid Substitution Character”
const result = "abc".replace(/./, "$x");
const matchedSubstring = "abc".replace(/./, "$&");
const firstGroup = "abc".replace(/(.)/, "$1");
const namedGroup = "abc".replace(/(?<char>.)/, "$<char>");
const literalDollar = "abc".replace(/./, "$$");

This rule is not configurable.

If you intentionally rely on the behavior where $ followed by an unrecognized character produces a literal $, you might prefer to disable this rule.

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