sindresorhus/eslint-plugin-unicorn

Rule proposal: Prefer `String#matchAll()`

Open

#574 创建于 2020年3月3日

在 GitHub 查看
 (6 评论) (5 反应) (0 负责人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

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.

贡献者指南