]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-invalid-regexp.md
import eslint 7.18.0
[pve-eslint.git] / eslint / docs / rules / no-invalid-regexp.md
CommitLineData
eb39fafa
DC
1# disallow invalid regular expression strings in `RegExp` constructors (no-invalid-regexp)
2
3An 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
7This rule disallows invalid regular expression strings in `RegExp` constructors.
8
9Examples of **incorrect** code for this rule:
10
11```js
12/*eslint no-invalid-regexp: "error"*/
13
14RegExp('[')
15
16RegExp('.', 'z')
17
18new RegExp('\\')
19```
20
21Examples of **correct** code for this rule:
22
23```js
24/*eslint no-invalid-regexp: "error"*/
25
26RegExp('.')
27
28new RegExp
29
30this.RegExp('[')
31```
32
456be15e 33Please note that this rule validates regular expressions per the latest ECMAScript specification, regardless of your parser settings.
eb39fafa 34
456be15e 35If 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.
eb39fafa
DC
36
37## Options
38
39This rule has an object option for exceptions:
40
41* `"allowConstructorFlags"` is an array of flags
42
43### allowConstructorFlags
44
456be15e 45Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["a", "z"] }` option:
eb39fafa
DC
46
47```js
456be15e 48/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/
eb39fafa 49
456be15e 50new RegExp('.', 'a')
eb39fafa 51
456be15e 52new RegExp('.', 'az')
eb39fafa
DC
53```
54
55## Further Reading
56
57* [Annotated ES5 ยง7.8.5 - Regular Expression Literals](https://es5.github.io/#x7.8.5)