2022-02-04 16:28:24 +00:00

27 lines
666 B
Plaintext

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
[source,javascript]
----
new RegExp(/foo/);
new RegExp('bar');
new RegExp('baz', 'i');
new RegExp("\\d+");
new RegExp(`qux|quuz`);
----
== Compliant Solution
[source,javascript]
----
/foo/;
/bar/;
/baz/i;
/\d+/;
/qux|quuz/;
new RegExp(`Dear ${title},`);
----