Regular expression should not be wrapped as a string at String.prototype.replace()
- BAD_REGEXP_LITERAL_IN_STRING
- Error
- Medium
- No tags
This rule applies when a regular expression literal is wrapped as a string at the first argument of String.prototype.replace()
.
String.prototype.replace()
accepts string or regular expression as the first argument. String argument is searched verbatim whereas matching pattern is searched for regular expression argument.
When a regular expression literal is wrapped with quotes like '/\s+/g'
, it will be recognized as a string argument and searched literally. This is not likely to be a programmer's intent.
This rule also applies to the analogous String.prototype.split()
.
Noncompliant Code Example
View with compliant examples side by sidefoo = foo.replace('/\s+/g', '_'); // BAD_REGEXP_LITERAL_IN_STRING alarm because quotes are used.
Compliant Code Example
View with noncompliant examples side by sidefoo = foo.replace(/\s+/g, '_');
Version
This rule was introduced in DeepScan 1.30.0.