我想在一个option1, option2字符串之后选择所有fromHere等。但是我最终只选择了最后一个出现的(option4

regex101

这是例子

正则表达式:/(?=fromHere(.+((option\d))))/g

文本:

    Ok ignoring this whole line option1 ignore


    fromHere I want to match all option1 ignore option2 ignore option3 xxxx
 option4  but got only the last one (4)


我找不到任何解决方案。

最佳答案

您可以将String.prototype.split()RegExp /fromHere一起使用,以拆分字符串以从结果数组中删除"fromHere"Array.prototype.pop()获取数组的索引1String.prototype.match()RegExp /option\d/g匹配在字符串中。



let str = `    Ok ignoring this whole line option1 ignore


    fromHere I want to match all option1 ignore option2 ignore option3 xxxx
 option4  but got only the last one (4)`;

let res = str.split(/fromHere/).pop().match(/option\d/g);

console.log(res);

关于javascript - 如何使用正则表达式在特定起点(单词)之后多次匹配一个单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45048396/

10-13 00:41