sindresorhus/eslint-plugin-unicorn

Rule proposal: Prefer `String#matchAll()`

Open

#574 opened on Mar 3, 2020

View on GitHub
 (6 comments) (5 reactions) (0 assignees)JavaScript (5,022 stars) (468 forks)user submission
help wantednew rule

Description

Not offering a PR here, but just thought such a rule could possibly fit your project...

Namely, preferring String.prototype.matchAll to while loop exec assignments.

As per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll#Examples , one can replace this:

let match;
while ((match = regexp.exec(str)) !== null) {
}

..with the likes of:

for (const match of str.matchAll(regexp)) {
}

I find it quite a bit more elegant and semantic, and it allows avoiding defining a variable in the parent block.

If not, no worries, just thought I'd see if it might pique the interest of others.

Contributor guide