1 # Disallow use of the `RegExp` constructor in favor of regular expression literals (prefer-regex-literals)
3 There are two ways to create a regular expression:
5 * Regular expression literals, e.g., `/abc/u`.
6 * The `RegExp` constructor function, e.g., `new RegExp("abc", "u")` or `RegExp("abc", "u")`.
8 The constructor function is particularly useful when you want to dynamically generate the pattern,
9 because it takes string arguments.
11 When using the constructor function with string literals, don't forget that the string escaping rules still apply.
12 If you want to put a backslash in the pattern, you need to escape it in the string literal.
13 Thus, the following are equivalent:
16 new RegExp("^\\d\\.$");
20 // matches "0.", "1.", "2." ... "9."
23 In the above example, the regular expression literal is easier to read and reason about.
24 Also, it's a common mistake to omit the extra `\` in the string literal, which would produce a completely different regular expression:
29 // equivalent to /^d.$/, matches "d1", "d2", "da", "db" ...
32 When a regular expression is known in advance, it is considered a best practice to avoid the string literal notation on top
33 of the regular expression notation, and use regular expression literals instead of the constructor function.
37 This rule disallows the use of the `RegExp` constructor function with string literals as its arguments.
39 This rule also disallows the use of the `RegExp` constructor function with template literals without expressions
40 and `String.raw` tagged template literals without expressions.
42 The rule does not disallow all use of the `RegExp` constructor. It should be still used for
43 dynamically generated regular expressions.
45 Examples of **incorrect** code for this rule:
48 /*eslint prefer-regex-literals: "error"*/
52 new RegExp("abc", "u");
58 new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");
62 new RegExp(String.raw`^\d\.$`);
65 Examples of **correct** code for this rule:
68 /*eslint prefer-regex-literals: "error"*/
74 /\d\d\.\d\d\.\d\d\d\d/;
78 // RegExp constructor is allowed for dynamically generated regular expressions
84 new RegExp(prefix + "abc");
86 RegExp(`${prefix}abc`);
88 new RegExp(String.raw`^\d\. ${suffix}`);
93 This rule has an object option:
95 * `disallowRedundantWrapping` set to `true` additionally checks for unnecessarily wrapped regex literals (Default `false`).
97 ### `disallowRedundantWrapping`
99 By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call. When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns.
101 Examples of `incorrect` code for `{ "disallowRedundantWrapping": true }`
104 /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
108 new RegExp(/abc/, 'u');
111 Examples of `correct` code for `{ "disallowRedundantWrapping": true }`
114 /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
120 new RegExp(/abc/, flags);
125 * [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
126 * [MDN: RegExp Constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)