]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-invalid-regexp.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-invalid-regexp.md
1 # disallow invalid regular expression strings in `RegExp` constructors (no-invalid-regexp)
2
3 An invalid pattern in a regular expression literal is a `SyntaxError` when the code is parsed, but an invalid string in `RegExp` constructors throws a `SyntaxError` only when the code is executed.
4
5 ## Rule Details
6
7 This rule disallows invalid regular expression strings in `RegExp` constructors.
8
9 Examples of **incorrect** code for this rule:
10
11 ```js
12 /*eslint no-invalid-regexp: "error"*/
13
14 RegExp('[')
15
16 RegExp('.', 'z')
17
18 new RegExp('\\')
19 ```
20
21 Examples of **correct** code for this rule:
22
23 ```js
24 /*eslint no-invalid-regexp: "error"*/
25
26 RegExp('.')
27
28 new RegExp
29
30 this.RegExp('[')
31 ```
32
33 Please note that this rule validates regular expressions per the latest ECMAScript specification, regardless of your parser settings.
34
35 If you want to allow additional constructor flags for any reason, you can specify them using the `allowConstructorFlags` option. These flags will then be ignored by the rule.
36
37 ## Options
38
39 This rule has an object option for exceptions:
40
41 * `"allowConstructorFlags"` is an array of flags
42
43 ### allowConstructorFlags
44
45 Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["a", "z"] }` option:
46
47 ```js
48 /*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/
49
50 new RegExp('.', 'a')
51
52 new RegExp('.', 'az')
53 ```
54
55 ## Further Reading
56
57 * [Annotated ES5 ยง7.8.5 - Regular Expression Literals](https://es5.github.io/#x7.8.5)