Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js matchall

作者:枫尘于往逝   发布日期:2026-01-29   浏览:26

// 使用 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

大家都在看

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 数组对象合并

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站