`prefer-named-capture-group` should be disabled when using `.test()` or `.replace()`
See original GitHub issueWhat rule do you want to change?
prefer-named-capture-group
suggested in https://github.com/eslint/eslint/issues/11381
Does this change cause the rule to produce more or fewer warnings?
Fewer warnings
How will the change be implemented? (New option, new default behavior, etc.)?
New default behavior
Please provide some example code that this change will affect:
const hasPlanet = /with (mars|pluto)/i.test(object);
return string.replace(/on (mars|venus)/gi, 'on Pluto');
What does the rule currently do for this code?
The rule suggests adding ?:
This has no advantages:
- the result is unchanged
- the group isn’t meant to be captured at all
- it lengthens the regex
.replace
can capture the groups but it doesn’t support named groups.
What will the rule do after it’s changed?
Not report an issue when a regex is used inline with .test
or .replace()
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
ESLint v8.0.0 released - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >Regex.Replace Method (System.Text.RegularExpressions)
In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.
Read more >String.prototype.replace() - JavaScript - MDN Web Docs
The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement .
Read more >"String#replace" should be preferred to "String#replaceAll"
'List.remove()' should not be used in ascending 'for' loops. Code Smell ... "Map.get" and value test should be replaced with single method call....
Read more >c# - String.Replace ignoring case - Stack Overflow
You could use a Regex and perform a case insensitive replace: class Program { static void Main() { string input = "hello WoRlD";...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think that
String.prototype.replace
supports named capture groupshttps://github.com/tc39/proposal-regexp-named-groups#replacement-targets
Thanks for this issue.
Personally, I think this is expected. You’re using regular expressions groups, but you aren’t going to capture anything, so this rule suggests you adding prefix
?:
which means using groups without capturing.