You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add prefer-lookaround rule
* Update lib/rules/prefer-lookaround.ts
Co-authored-by: Michael Schmidt <[email protected]>
* fix prefer-lookaround
* Update prefer-lookaround
* Update prefer-lookaround
* Add check to if it consume character
* Improve `prefer-lookaround` (#328)
* fix autofix of end capturing group
Co-authored-by: Michael Schmidt <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,6 +167,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
167
167
|[regexp/no-useless-non-capturing-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-non-capturing-group.html)| disallow unnecessary Non-capturing group |:star::wrench:|
168
168
|[regexp/prefer-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-character-class.html)| enforce using character class |:star::wrench:|
169
169
|[regexp/prefer-d](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-d.html)| enforce using `\d`|:star::wrench:|
170
+
|[regexp/prefer-lookaround](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-lookaround.html)| prefer lookarounds over capturing group that do not replace |:wrench:|
170
171
|[regexp/prefer-named-backreference](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-named-backreference.html)| enforce using named backreferences |:wrench:|
171
172
|[regexp/prefer-plus-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-plus-quantifier.html)| enforce using `+` quantifier |:star::wrench:|
172
173
|[regexp/prefer-question-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-question-quantifier.html)| enforce using `?` quantifier |:star::wrench:|
description: "prefer lookarounds over capturing group that do not replace"
6
+
---
7
+
# regexp/prefer-lookaround
8
+
9
+
> prefer lookarounds over capturing group that do not replace
10
+
11
+
-:exclamation: <badgetext="This rule has not been released yet."vertical="middle"type="error"> ***This rule has not been released yet.*** </badge>
12
+
-:wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
13
+
14
+
## :book: Rule Details
15
+
16
+
This rule reports string replacement using capturing groups that can be replaced with lookaround assertions.
17
+
18
+
<eslint-code-blockfix>
19
+
20
+
```js
21
+
/* eslint regexp/prefer-lookaround: "error" */
22
+
23
+
/* ✓ GOOD */
24
+
var str ='JavaScript'.replace(/Java(?=Script)/g, 'Type')
25
+
26
+
/* ✗ BAD */
27
+
var str ='JavaScript'.replace(/Java(Script)/g, 'Type$1')
28
+
```
29
+
30
+
</eslint-code-block>
31
+
32
+
## :wrench: Options
33
+
34
+
```json
35
+
{
36
+
"regexp/prefer-lookaround": ["error", {
37
+
"strictTypes": true
38
+
}]
39
+
}
40
+
```
41
+
42
+
-`strictTypes` ... If `true`, strictly check the type of object to determine if the regex instance was used in `replace()` and `replaceAll()`. Default is `true`.
0 commit comments