Node.js 是否支持 String.MatchAll 方法?

Kal*_*nGi 13 regex node.js

当我测试代码时:

let result = 'heymama'.matchAll(/m(a)/g);
Run Code Online (Sandbox Code Playgroud)

我收到错误“ 'heymama'.matchAll 不是函数

当我运行版本时:

let result = 'heymama'.match(/ma/g);
Run Code Online (Sandbox Code Playgroud)

没有错误。

Den*_*ash 14

#String.matchAllNode.js从版本支持12.0.0

查看MDN 上的兼容性

在此处输入图片说明


Pie*_*eau 14

matchAll 从 v12.0.0 开始在 Node.js 中可用


Chr*_*zon 7

正如@dennis-vash alredy 指出的那样,Node.js 目前不支持它。

不过有这个“匹配所有”npm 包的替代方案。

const matchAll = require("match-all");

let s = "Hello _World_ and _Mars_";
console.log(matchAll(s, /_([a-z]+)_/gi).toArray());
// => [ "World", "Mars" ]
Run Code Online (Sandbox Code Playgroud)