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