Regular expression literals should be preferred over the RegExp
constructor calls when the pattern is a literal. Simply using a
regular expression literal is more concise and easier to read and does not require escaping like a string literal does.
Using the RegExp
constructor is suitable when the pattern is computed dynamically, e.g. when it is provided by the user.
Noncompliant Code Example
new RegExp(/foo/);
new RegExp('bar');
new RegExp('baz', 'i');
new RegExp("\\d+");
new RegExp(`qux|quuz`);
Compliant Solution
/foo/;
/bar/;
/baz/i;
/\d+/;
/qux|quuz/;
new RegExp(`Dear ${title},`);