// 使用 matchAll 方法的示例代码
const regex = /t(e)(st(\d?))/g;
const str = 'test1 test2';
const matches = str.matchAll(regex);
for (const match of matches) {
console.log(match);
}
// 输出:
// [ 'test1', 'e', 'st1', '1', index: 0, input: 'test1 test2', groups: undefined ]
// [ 'test2', 'e', 'st2', '2', index: 6, input: 'test1 test2', groups: undefined ]
matchAll 方法返回一个迭代器,该迭代器会生成所有与正则表达式匹配的结果。/t(e)(st(\d?))/g,它用于匹配字符串中的模式。str.matchAll(regex) 返回一个迭代器,我们可以使用 for...of 循环来遍历所有的匹配结果。index(匹配项在原字符串中的索引)、input(原始字符串)和 groups(命名捕获组)。上一篇:js isfinite
下一篇:js nexttick
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站