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.

plugin-regexp/remove-useless-group should not remove groups in `.split()` or `.replace()`

See original GitHub issue

Issue Description

plugin-regexp/remove-useless-group changes:

'lala'.split(/(a)/g); // Output: ["l", "a", "l", "a", ""]


'lala'.split(/a/g); // Output: ["l", "l", ""]

It also changes:

'lala'.replace(/(a)/g,'-$1-'); // Output: "l-a-l-a-"


'lala'.replace(/a/g,'-$1-'); // Output: "l-$1-l-$1-"

This plugin is safe to use in .test(), but should likely ignore regex that appears in any other function (match, split, replace).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

egilllcommented, Aug 23, 2021

Wonderful, now works correctly when inside of a split or replace. There are three other situations I’ve been able to find where this results in changing the output:

  1. Standalone regex
// Before
const r = /(a)/g;
"lala".split(r);  // Output: ["l", "a", "l", "a", ""]
// After
const r = /a/g;
"lala".split(r); // Output: ["l", "l", ""]
  1. exec()
// Before
/(a)/g.exec("haha"); // Output: ["a", "a"]
// After
/a/g.exec("haha"); // Output: ["a"]
  1. replaceAll()
// Before
"lala".replaceAll(/(a)/g,'-$1-'); // Output: "l-a-l-a-"
// After
"lala".replaceAll(/a/g, "-$1-"); // Output: "l-$1-l-$1-"

A possible solution (in particular to the standalone regex) might be to whitelist regex that is:

  • Directly followed by .test()
  • Directly followed by .search()
  • Inside of .match()

instead of blacklisting?

(By the way, this is a great project that I’ve been much in need of!)

coderaisercommented, Aug 31, 2021

Landed in @putout/plugin-regexp@2.8.0 🎉 . Is it works for you?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace ignores non-capture group - regex - Stack Overflow
Regardless of whether you create a capture or not, .Replace() will always replace the whole match. From MSDN: Regex.Replace Method (String ...
Read more >
Capturing groups - The Modern JavaScript Tutorial
Method str.replace(regexp, replacement) that replaces all matches with regexp in str allows to use parentheses contents in the replacement ...
Read more >
SplitChunksPlugin - webpack
Webpack will automatically split chunks based on these conditions: New chunk can be shared OR modules are from the node_modules folder; New chunk...
Read more >
Searching - Notepad++ User Manual
Searching. There are multiple methods to search (and replace) text in files. You can also mark search results with a bookmark on their...
Read more >
perlretut - Perl regular expressions tutorial - Perldoc Browser
The first regexp world doesn't match because regexps are by default case-sensitive. The second regexp matches because the substring 'o W' occurs in...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found