]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: prefer-regex-literals | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | further_reading: | |
5 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions | |
6 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | |
7 | --- | |
8 | ||
9 | ||
eb39fafa DC |
10 | |
11 | There are two ways to create a regular expression: | |
12 | ||
13 | * Regular expression literals, e.g., `/abc/u`. | |
14 | * The `RegExp` constructor function, e.g., `new RegExp("abc", "u")` or `RegExp("abc", "u")`. | |
15 | ||
16 | The constructor function is particularly useful when you want to dynamically generate the pattern, | |
17 | because it takes string arguments. | |
18 | ||
19 | When using the constructor function with string literals, don't forget that the string escaping rules still apply. | |
20 | If you want to put a backslash in the pattern, you need to escape it in the string literal. | |
21 | Thus, the following are equivalent: | |
22 | ||
23 | ```js | |
24 | new RegExp("^\\d\\.$"); | |
25 | ||
26 | /^\d\.$/; | |
27 | ||
28 | // matches "0.", "1.", "2." ... "9." | |
29 | ``` | |
30 | ||
31 | In the above example, the regular expression literal is easier to read and reason about. | |
32 | Also, it's a common mistake to omit the extra `\` in the string literal, which would produce a completely different regular expression: | |
33 | ||
34 | ```js | |
35 | new RegExp("^\d\.$"); | |
36 | ||
37 | // equivalent to /^d.$/, matches "d1", "d2", "da", "db" ... | |
38 | ``` | |
39 | ||
40 | When a regular expression is known in advance, it is considered a best practice to avoid the string literal notation on top | |
41 | of the regular expression notation, and use regular expression literals instead of the constructor function. | |
42 | ||
43 | ## Rule Details | |
44 | ||
45 | This rule disallows the use of the `RegExp` constructor function with string literals as its arguments. | |
46 | ||
47 | This rule also disallows the use of the `RegExp` constructor function with template literals without expressions | |
48 | and `String.raw` tagged template literals without expressions. | |
49 | ||
50 | The rule does not disallow all use of the `RegExp` constructor. It should be still used for | |
51 | dynamically generated regular expressions. | |
52 | ||
53 | Examples of **incorrect** code for this rule: | |
54 | ||
8f9d1d4d DC |
55 | ::: incorrect |
56 | ||
eb39fafa DC |
57 | ```js |
58 | /*eslint prefer-regex-literals: "error"*/ | |
59 | ||
60 | new RegExp("abc"); | |
61 | ||
62 | new RegExp("abc", "u"); | |
63 | ||
64 | RegExp("abc"); | |
65 | ||
66 | RegExp("abc", "u"); | |
67 | ||
68 | new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d"); | |
69 | ||
70 | RegExp(`^\\d\\.$`); | |
71 | ||
72 | new RegExp(String.raw`^\d\.$`); | |
73 | ``` | |
74 | ||
8f9d1d4d DC |
75 | ::: |
76 | ||
eb39fafa DC |
77 | Examples of **correct** code for this rule: |
78 | ||
8f9d1d4d DC |
79 | ::: correct |
80 | ||
eb39fafa DC |
81 | ```js |
82 | /*eslint prefer-regex-literals: "error"*/ | |
83 | ||
84 | /abc/; | |
85 | ||
86 | /abc/u; | |
87 | ||
88 | /\d\d\.\d\d\.\d\d\d\d/; | |
89 | ||
90 | /^\d\.$/; | |
91 | ||
92 | // RegExp constructor is allowed for dynamically generated regular expressions | |
93 | ||
94 | new RegExp(pattern); | |
95 | ||
96 | RegExp("abc", flags); | |
97 | ||
98 | new RegExp(prefix + "abc"); | |
99 | ||
100 | RegExp(`${prefix}abc`); | |
101 | ||
102 | new RegExp(String.raw`^\d\. ${suffix}`); | |
103 | ``` | |
104 | ||
8f9d1d4d DC |
105 | ::: |
106 | ||
6f036462 TL |
107 | ## Options |
108 | ||
109 | This rule has an object option: | |
110 | ||
111 | * `disallowRedundantWrapping` set to `true` additionally checks for unnecessarily wrapped regex literals (Default `false`). | |
112 | ||
113 | ### `disallowRedundantWrapping` | |
114 | ||
115 | 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. | |
116 | ||
117 | Examples of `incorrect` code for `{ "disallowRedundantWrapping": true }` | |
118 | ||
119 | ```js | |
120 | /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/ | |
121 | ||
122 | new RegExp(/abc/); | |
123 | ||
124 | new RegExp(/abc/, 'u'); | |
125 | ``` | |
126 | ||
127 | Examples of `correct` code for `{ "disallowRedundantWrapping": true }` | |
128 | ||
129 | ```js | |
130 | /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/ | |
131 | ||
132 | /abc/; | |
133 | ||
134 | /abc/u; | |
135 | ||
136 | new RegExp(/abc/, flags); | |
137 | ``` |