# regex-complexity

Rule `sonarjs/regex-complexity`. Regular expressions should not be too complicated

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

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
const datePattern = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
if (dateString.match(datePattern)) {
    handleDate(dateString);
}
```

### Correct: the rule accepts this

```js
const datePattern = /^\d{1,2}([-/.])\d{1,2}\1\d{1,4}$/;
if (dateString.match(datePattern)) {
    const dateParts = dateString.split(/[-/.]/);
    const day = parseInt(dateParts[0]);
    const month = parseInt(dateParts[1]);
    const year = parseInt(dateParts[2]);
    // Put logic to validate and process the date based on its integer parts here
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/regex-complexity": "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/redundant-type-aliases](https://lintradar.dev/rules/sonarjs/redundant-type-aliases.md): Redundant type aliases should not be used
- [sonarjs/review-blockchain-mnemonic](https://lintradar.dev/rules/sonarjs/review-blockchain-mnemonic.md): Wallet phrases should not be hard-coded
- [sonarjs/reduce-initial-value](https://lintradar.dev/rules/sonarjs/reduce-initial-value.md): "Array.reduce()" calls should include an initial value
- [sonarjs/session-regeneration](https://lintradar.dev/rules/sonarjs/session-regeneration.md): A new session should be created during user authentication
- [sonarjs/publicly-writable-directories](https://lintradar.dev/rules/sonarjs/publicly-writable-directories.md): Temporary files should not be created in publicly writable directories
- [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

---

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