# existing-groups

Rule `sonarjs/existing-groups`. Replacement strings should reference existing regular expression groups

- 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: #1211 by reach, #512 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
const str = 'James Bond';
console.log(str.replace(/(\w+)\s(\w+)/, '$1, $0 $1')); // Noncompliant, index is 1-based, '$0' does not exist, prints 'James, $0 James'
console.log(str.replace(/(?<firstName>\w+)\s(?<lastName>\w+)/, '$<surname>, $<firstName> $<surname>')); // Noncompliant  '$<surname>' does not exist, prints ', James '
```

### Correct: the rule accepts this

```js
const str = 'James Bond';
console.log(str.replace(/(\w+)\s(\w+)/, '$2, $1 $2'));
console.log(str.replace(/(?<firstName>\w+)\s(?<lastName>\w+)/, '$<lastName>, $<firstName> $<lastName>'));
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/existing-groups": "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/encryption-secure-mode](https://lintradar.dev/rules/sonarjs/encryption-secure-mode.md): Encryption algorithms should be used with secure mode and padding scheme
- [sonarjs/explicit-test-skip](https://lintradar.dev/rules/sonarjs/explicit-test-skip.md): Tests should be skipped explicitly
- [sonarjs/empty-string-repetition](https://lintradar.dev/rules/sonarjs/empty-string-repetition.md): Repeated patterns in regular expressions should not match the empty string
- [sonarjs/file-permissions](https://lintradar.dev/rules/sonarjs/file-permissions.md): File permissions should not be set to world-accessible values
- [sonarjs/dynamically-constructed-templates](https://lintradar.dev/rules/sonarjs/dynamically-constructed-templates.md): Templates should not be constructed dynamically
- [sonarjs/file-uploads](https://lintradar.dev/rules/sonarjs/file-uploads.md): File uploads should be restricted

---

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