]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/prefer-regex-literals.md
2ba8cacc08d4e86adb70b52bb37f09b041334b27
[pve-eslint.git] / eslint / docs / rules / prefer-regex-literals.md
1 # Disallow use of the `RegExp` constructor in favor of regular expression literals (prefer-regex-literals)
2
3 There are two ways to create a regular expression:
4
5 * Regular expression literals, e.g., `/abc/u`.
6 * The `RegExp` constructor function, e.g., `new RegExp("abc", "u")` or `RegExp("abc", "u")`.
7
8 The constructor function is particularly useful when you want to dynamically generate the pattern,
9 because it takes string arguments.
10
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:
14
15 ```js
16 new RegExp("^\\d\\.$");
17
18 /^\d\.$/;
19
20 // matches "0.", "1.", "2." ... "9."
21 ```
22
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:
25
26 ```js
27 new RegExp("^\d\.$");
28
29 // equivalent to /^d.$/, matches "d1", "d2", "da", "db" ...
30 ```
31
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.
34
35 ## Rule Details
36
37 This rule disallows the use of the `RegExp` constructor function with string literals as its arguments.
38
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.
41
42 The rule does not disallow all use of the `RegExp` constructor. It should be still used for
43 dynamically generated regular expressions.
44
45 Examples of **incorrect** code for this rule:
46
47 ```js
48 /*eslint prefer-regex-literals: "error"*/
49
50 new RegExp("abc");
51
52 new RegExp("abc", "u");
53
54 RegExp("abc");
55
56 RegExp("abc", "u");
57
58 new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");
59
60 RegExp(`^\\d\\.$`);
61
62 new RegExp(String.raw`^\d\.$`);
63 ```
64
65 Examples of **correct** code for this rule:
66
67 ```js
68 /*eslint prefer-regex-literals: "error"*/
69
70 /abc/;
71
72 /abc/u;
73
74 /\d\d\.\d\d\.\d\d\d\d/;
75
76 /^\d\.$/;
77
78 // RegExp constructor is allowed for dynamically generated regular expressions
79
80 new RegExp(pattern);
81
82 RegExp("abc", flags);
83
84 new RegExp(prefix + "abc");
85
86 RegExp(`${prefix}abc`);
87
88 new RegExp(String.raw`^\d\. ${suffix}`);
89 ```
90
91 ## Options
92
93 This rule has an object option:
94
95 * `disallowRedundantWrapping` set to `true` additionally checks for unnecessarily wrapped regex literals (Default `false`).
96
97 ### `disallowRedundantWrapping`
98
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.
100
101 Examples of `incorrect` code for `{ "disallowRedundantWrapping": true }`
102
103 ```js
104 /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
105
106 new RegExp(/abc/);
107
108 new RegExp(/abc/, 'u');
109 ```
110
111 Examples of `correct` code for `{ "disallowRedundantWrapping": true }`
112
113 ```js
114 /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
115
116 /abc/;
117
118 /abc/u;
119
120 new RegExp(/abc/, flags);
121 ```
122
123 ## Further Reading
124
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)