# slow-regex

Rule `sonarjs/slow-regex`. Regular expressions should not cause catastrophic backtracking

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: yes
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1363 by reach, #664 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/slow-regex` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-5852/) |

## Code examples

### Correct: the rule accepts this

```js
/((?=(a+))\2)+$/.test(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+
"aaaaaaaaaaaaaaa!"
); // Compliant
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/slow-regex": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/single-character-alternation](https://lintradar.dev/rules/sonarjs/single-character-alternation.md): Single-character alternations in regular expressions should be replaced with character classes
- [sonarjs/sql-queries](https://lintradar.dev/rules/sonarjs/sql-queries.md): SQL queries should not be dynamically formatted
- [sonarjs/single-char-in-character-classes](https://lintradar.dev/rules/sonarjs/single-char-in-character-classes.md): Character classes in regular expressions should not contain only one character
- [sonarjs/stable-tests](https://lintradar.dev/rules/sonarjs/stable-tests.md): Tests should be stable
- [sonarjs/session-regeneration](https://lintradar.dev/rules/sonarjs/session-regeneration.md): A new session should be created during user authentication
- [sonarjs/stateful-regex](https://lintradar.dev/rules/sonarjs/stateful-regex.md): Regular expressions with the global flag should be used with caution

---

Web page: https://lintradar.dev/rules/sonarjs/slow-regex. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
