Skip to content

regexDollarEscapes

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

✅ This rule is included in the ts stylistic and stylisticStrict 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
const result: string
result
= "€100".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(//, "$");

Dollar Before Invalid Substitution Character

Section titled “Dollar Before Invalid Substitution Character”
const
const result: string
result
= "abc".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/./, "$x");
const
const matchedSubstring: string
matchedSubstring
= "abc".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/./, "$&");
const
const firstGroup: string
firstGroup
= "abc".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/(.)/, "$1");
const
const namedGroup: string
namedGroup
= "abc".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/(?<char>.)/, "$<char>");
const
const literalDollar: string
literalDollar
= "abc".
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

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 ❤️‍🔥 around the world by the Flint team and contributors.