33 lines
764 B
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-20 09:43:42 +01:00
Using regular expression literals is recommended over using the `RegExp` constructor calls if the pattern is a literal. Regular expression literals are shorter, more readable, and do not need to be escaped like string literals.
2022-02-04 17:28:24 +01:00
[source,javascript]
----
new RegExp(/foo/);
new RegExp('bar');
new RegExp('baz', 'i');
new RegExp("\\d+");
new RegExp(`qux|quuz`);
----
2023-06-20 09:43:42 +01:00
Using the `RegExp` constructor is suitable when the pattern is computed dynamically, for example when it is provided by the user.
2022-02-04 17:28:24 +01:00
[source,javascript]
----
/foo/;
/bar/;
/baz/i;
/\d+/;
/qux|quuz/;
new RegExp(`Dear ${title},`);
----
2023-06-20 09:43:42 +01:00
== Resources
=== Documentation
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp[MDN - RegExp]