Skip to content

matchAll 全局修饰符 #992

@mgsers

Description

@mgsers

https://es6.ruanyifeng.com/#docs/regex#String-prototype-matchAll

const string = 'test1test2test3';

// g 修饰符加不加都可以
const regex = /t(e)(st(\d?))/g;

for (const match of string.matchAll(regex)) {
  console.log(match);
}
// ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"]
// ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"]
// ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]

老师,正则表达式那里如果去掉全局修饰符的话chrome浏览是会报

String.prototype.matchAll called with a non-global RegExp argument

错误的,所以不应该是 g 修饰符加不加都可以
chrome版本83

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions