# empty-string-repetition

Rule `sonarjs/empty-string-repetition`. Repeated patterns in regular expressions should not match the empty string

- 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: #1209 by reach, #510 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
/(?:)*/      // same as the empty regex, the '*' accomplishes nothing
/(?:|x)*/    // same as the empty regex, the alternative has no effect
/(?:x|)*/    // same as 'x*', the empty alternative has no effect
/(?:x*|y*)*/ // same as 'x*', the first alternative would always match, y* is never tried
/(?:x?)*/    // same as 'x*'
/(?:x?)+/    // same as 'x*'
```

### Correct: the rule accepts this

```js
/x*/
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/empty-string-repetition": "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/dynamically-constructed-templates](https://lintradar.dev/rules/sonarjs/dynamically-constructed-templates.md): Templates should not be constructed dynamically
- [sonarjs/encryption-secure-mode](https://lintradar.dev/rules/sonarjs/encryption-secure-mode.md): Encryption algorithms should be used with secure mode and padding scheme
- [sonarjs/duplicates-in-character-class](https://lintradar.dev/rules/sonarjs/duplicates-in-character-class.md): Character classes in regular expressions should not contain the same character twice
- [sonarjs/existing-groups](https://lintradar.dev/rules/sonarjs/existing-groups.md): Replacement strings should reference existing regular expression groups
- [sonarjs/dompurify-unsafe-config](https://lintradar.dev/rules/sonarjs/dompurify-unsafe-config.md): DOMPurify configuration should not be bypassable
- [sonarjs/explicit-test-skip](https://lintradar.dev/rules/sonarjs/explicit-test-skip.md): Tests should be skipped explicitly

---

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