# stateful-regex

Rule `sonarjs/stateful-regex`. Regular expressions with the global flag should be used with caution

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

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
const datePattern = /\d{4}-\d{2}-\d{2}/g;
datePattern.test('2020-08-06');
datePattern.test('2019-10-10'); // Noncompliant: the regex will return "false" despite the date being well-formed

const str = 'foodie fooled football';
while ((result = /foo*/g.exec(str)) !== null) { // Noncompliant: a regex is defined at each iteration causing an infinite loop
  /* ... */
}

const stickyPattern = /abc/gy; // Noncompliant: a regex defined as both sticky and global ignores the global flag
stickyPattern.test(/* ... */);
```

### Correct: the rule accepts this

```js
const datePattern = /\d{4}-\d{2}-\d{2}/;
datePattern.test('2020-08-06');
datePattern.test('2019-10-10'); // Compliant

const reg = /foo*/g;
const str = 'foodie fooled football';
while ((result = reg.exec(str)) !== null) { // Compliant
  /* ... */
}

const stickyPattern = /abc/y; // Compliant
stickyPattern.test(/* ... */);
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/stateful-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/stable-tests](https://lintradar.dev/rules/sonarjs/stable-tests.md): Tests should be stable
- [sonarjs/strict-transport-security](https://lintradar.dev/rules/sonarjs/strict-transport-security.md): HTTP Strict-Transport-Security policy should not be disabled
- [sonarjs/sql-queries](https://lintradar.dev/rules/sonarjs/sql-queries.md): SQL queries should not be dynamically formatted
- [sonarjs/super-linear-regex](https://lintradar.dev/rules/sonarjs/super-linear-regex.md): Regular expressions should not cause non-linear backtracking
- [sonarjs/slow-regex](https://lintradar.dev/rules/sonarjs/slow-regex.md): Regular expressions should not cause catastrophic backtracking
- [sonarjs/synchronous-exception-assertions](https://lintradar.dev/rules/sonarjs/synchronous-exception-assertions.md): Synchronous exception assertions should receive synchronous callbacks

---

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