mirror of
https://git.proxmox.com/git/pve-eslint
synced 2025-10-06 04:47:54 +00:00
58 lines
1.4 KiB
Markdown
58 lines
1.4 KiB
Markdown
# disallow invalid regular expression strings in `RegExp` constructors (no-invalid-regexp)
|
|
|
|
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.
|
|
|
|
## Rule Details
|
|
|
|
This rule disallows invalid regular expression strings in `RegExp` constructors.
|
|
|
|
Examples of **incorrect** code for this rule:
|
|
|
|
```js
|
|
/*eslint no-invalid-regexp: "error"*/
|
|
|
|
RegExp('[')
|
|
|
|
RegExp('.', 'z')
|
|
|
|
new RegExp('\\')
|
|
```
|
|
|
|
Examples of **correct** code for this rule:
|
|
|
|
```js
|
|
/*eslint no-invalid-regexp: "error"*/
|
|
|
|
RegExp('.')
|
|
|
|
new RegExp
|
|
|
|
this.RegExp('[')
|
|
```
|
|
|
|
Please note that this rule validates regular expressions per the latest ECMAScript specification, regardless of your parser settings.
|
|
|
|
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.
|
|
|
|
## Options
|
|
|
|
This rule has an object option for exceptions:
|
|
|
|
* `"allowConstructorFlags"` is an array of flags
|
|
|
|
### allowConstructorFlags
|
|
|
|
Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["a", "z"] }` option:
|
|
|
|
```js
|
|
/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/
|
|
|
|
new RegExp('.', 'a')
|
|
|
|
new RegExp('.', 'az')
|
|
```
|
|
|
|
## Further Reading
|
|
|
|
* [Annotated ES5 §7.8.5 - Regular Expression Literals](https://es5.github.io/#x7.8.5)
|