1 使用match 不能加/g 否则无效

2 使用matchAll  需要先定义一个RegExp对象

const regexp = RegExp(/,"(.+)"\)/,'g');
const matches = str.matchAll(regexp);
for (const match of matches) {
  console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`);
  console.log(match[1]);
}

可以使用exec

const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
let match;

while ((match = regexp.exec(str)) !== null) {
  console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`);
  // expected output: "Found football start=6 end=14."
  // expected output: "Found foosball start=16 end=24."
}

 

 

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2021-06-01
  • 2021-06-16
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
猜你喜欢
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-04-23
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案