# no-invalid-regexp

> Disallow invalid patterns and flags in `RegExp()` and `new RegExp()`

A mistake in a regular expression literal is a syntax error, so you see it right away. A pattern passed to `RegExp` as a string is only checked when that line runs, and then it throws a `SyntaxError`. This rule checks the string ahead of time.

Only string literals are checked. If the pattern or the flags come from a variable, that part is skipped.

This rule is on by default.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"no-invalid-regexp" = "off" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"no-invalid-regexp" = ["error", {
  allowConstructorFlags = [], # list of strings
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 4} */}
```js
const letters = new RegExp("[a-z");
const digits = RegExp("(\\d+");
const twice = new RegExp("\\d+", "gg");
const unknown = new RegExp("\\d+", "x");
```

Not reported:

{/* good */}
```js
const letters = new RegExp("[a-z]");
const digits = RegExp("(\\d+)", "g");
const search = new RegExp(pattern, flags);
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allowConstructorFlags` | `[]` | Extra flags to accept, one letter each, for a runtime that supports flags the standard doesn't have. Case matters. |

{/* good {"options": [{"allowConstructorFlags": ["x"]}]} */}
```js
const digits = new RegExp("\\d+", "x");
```

{/* bad {"options": [{"allowConstructorFlags": ["x"]}]} */}
```js
const digits = new RegExp("\\d+", "X");
```
