Reference JavaScript reference

String.prototype.matchAll 参考

String.prototype.matchAll Reference,整理语法、参数、示例、返回值和常见错误,适合快速查询 JavaScript 写法。

语法

js
const result = [...text.matchAll(/pattern/g)];

参数

  • text:要匹配的字符串。
  • pattern / flags:正则表达式模式和标志。

返回值

返回布尔值、匹配数组、迭代器或替换后的字符串。

示例

js
const result = [..."a1 b2".matchAll(/\w(\d)/g)];
console.log(result.map((item) => item[1]));

常见错误

  • 正则没有设置合适的 flags 时,可能只匹配到第一个结果或大小写不符合预期。

相关内容