gpt4 book ai didi

javascript - 如何使用正则表达式在特定起点(单词)之后多次匹配单词

转载 作者:行者123 更新时间:2023-11-29 21:03:57 26 4
gpt4 key购买 nike

我想在 fromHere 字符串之后选择所有 option1、option2 等,但我最终只选择了最后一次出现的 (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 匹配字符串中的 "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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com