The search and the replace strings of String.prototype.replace() should not be the same

  • FUTILE_STRING_REPLACE
  • Error
  • Medium
  • No tags

This rule applies when the search and the replace strings of String.prototype.replace() are the same.

When the two strings are the same, the replace result will be always the same as the input string. This may happen when characters like \ are not properly escaped in the search or the replace string.

Noncompliant Code Example

View with compliant examples side by side
foo = foo.replace(/"/g, '\"'); // FUTILE_STRING_REPLACE alarm because '\' is not properly escaped.

Compliant Code Example

View with noncompliant examples side by side
foo = foo.replace(/"/g, '\\"');

Version

This rule was introduced in DeepScan 1.31.0.

See

Was this documentation helpful?