我想在 fromHere
字符串之后选择所有 option1、option2
等,但我最终只选择最后一个出现的位置 (option4
)
这是示例
正则表达式:/(?=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()
获取数组的索引 1
,String.prototype.match()
与 RegExp
/option\d/g
匹配字符串中的"optionN"
。
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/