# no-empty-alternatives

Rule `sonarjs/no-empty-alternatives`. Alternation in regular expressions should not contain empty alternatives

- 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: #1260 by reach, #561 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
/Jack|Peter|/.test('John'); // Noncompliant - returns 'true'
/Jack||Peter/.test('John'); // Noncompliant - returns 'true'
```

### Correct: the rule accepts this

```js
/Jack|Peter/.test('John'); // returns 'false'
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-empty-alternatives": "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/no-empty-after-reluctant](https://lintradar.dev/rules/sonarjs/no-empty-after-reluctant.md): Reluctant quantifiers in regular expressions should be followed by an expression that can't match the empty string
- [sonarjs/no-empty-character-class](https://lintradar.dev/rules/sonarjs/no-empty-character-class.md): Empty character classes should not be used
- [sonarjs/no-element-overwrite](https://lintradar.dev/rules/sonarjs/no-element-overwrite.md): Collection elements should not be replaced unconditionally
- [sonarjs/no-empty-collection](https://lintradar.dev/rules/sonarjs/no-empty-collection.md): Empty collections should not be accessed or iterated
- [sonarjs/no-duplicated-branches](https://lintradar.dev/rules/sonarjs/no-duplicated-branches.md): Two branches in a conditional structure should not have exactly the same implementation
- [sonarjs/no-empty-group](https://lintradar.dev/rules/sonarjs/no-empty-group.md): Regular expressions should not contain empty groups

---

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