|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "regexp/no-unused-capturing-group" |
| 5 | +description: "disallow unused capturing group" |
| 6 | +--- |
| 7 | +# regexp/no-unused-capturing-group |
| 8 | + |
| 9 | +> disallow unused capturing group |
| 10 | +
|
| 11 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> |
| 12 | + |
| 13 | +## :book: Rule Details |
| 14 | + |
| 15 | +This rule aims to optimize regular expressions by replacing unused capturing groups with non-capturing groups. |
| 16 | + |
| 17 | +<eslint-code-block> |
| 18 | + |
| 19 | +```js |
| 20 | +/* eslint regexp/no-unused-capturing-group: "error" */ |
| 21 | + |
| 22 | +/* ✓ GOOD */ |
| 23 | +var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1/$2/$3') // "2000/12/31" |
| 24 | +var replaced = '2000-12-31'.replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/u, '$<y>/$<m>/$<d>') // "2000/12/31" |
| 25 | +var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, (_, y, m, d) => `${y}/${m}/${d}`) // "2000/12/31" |
| 26 | + |
| 27 | +var isDate = /(?:\d{4})-(?:\d{2})-(?:\d{2})/.test('2000-12-31') // true |
| 28 | + |
| 29 | +var matches = '2000-12-31 2001-01-01'.match(/(\d{4})-(\d{2})-(\d{2})/) |
| 30 | +var y = matches[1] // "2000" |
| 31 | +var m = matches[2] // "12" |
| 32 | +var d = matches[3] // "31" |
| 33 | + |
| 34 | +var index = '2000-12-31'.search(/(?:\d{4})-(?:\d{2})-(?:\d{2})/) // 0 |
| 35 | + |
| 36 | +/* ✗ BAD */ |
| 37 | +var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, 'Date') // "Date" |
| 38 | +var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1/$2') // "2000/12" |
| 39 | +var replaced = '2000-12-31'.replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/u, '$<y>/$<m>') // "2000/12" |
| 40 | +var replaced = '2000-12-31'.replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/u, '$1/$2/$3') // "2000/12/31" |
| 41 | +var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, (_, y, m) => `${y}/${m}`) // "2000/12" |
| 42 | + |
| 43 | +var isDate = /(\d{4})-(\d{2})-(\d{2})/.test('2000-12-31') // true |
| 44 | + |
| 45 | +var matches = '2000-12-31 2001-01-01'.match(/(\d{4})-(\d{2})-(\d{2})/g) // ["2000-12-31", "2001-01-01"] |
| 46 | + |
| 47 | +var index = '2000-12-31'.search(/(\d{4})-(\d{2})-(\d{2})/) // 0 |
| 48 | +``` |
| 49 | + |
| 50 | +</eslint-code-block> |
| 51 | + |
| 52 | +## :wrench: Options |
| 53 | + |
| 54 | +Nothing. |
| 55 | + |
| 56 | +## :couple: Related rules |
| 57 | + |
| 58 | +- [regexp/no-useless-dollar-replacements](./no-useless-dollar-replacements.md) |
| 59 | + |
| 60 | +## :mag: Implementation |
| 61 | + |
| 62 | +- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/no-unused-capturing-group.ts) |
| 63 | +- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/no-unused-capturing-group.ts) |
0 commit comments