question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-named-capture-group` should be disabled when using `.test()` or `.replace()`

See original GitHub issue

What 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:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
mdjermanoviccommented, Sep 27, 2019

I think that String.prototype.replace supports named capture groups

https://github.com/tc39/proposal-regexp-named-groups#replacement-targets

5reactions
g-planecommented, Sep 27, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found