英文:
How do you get the word - after a regular expression matched phrase
问题
我想获得匹配短语后面的下一个单词。
示例:我想要改变我的疼痛阈值
-- "改变我的 ***"
let text = "我想要改变我的疼痛阈值";
let firstTriggerregEx = "(?:\\b|')(改变我的)(?:\\b|')";
const found2 = text.match(firstTriggerregEx);
console.log(found2);
这将返回一个匹配,但不包括后面的单词。
英文:
I want to get the next word after the matched phrase.
e.t. - I want to change my pain threshold
-- "change my ***"
let text = "I want to change my pain threshold"
let firstTriggerregEx = "(?:\\b|')(change my)(?:\\b|')";
const found2 = text.match(firstTriggerregEx);
console.log(found2);
This will return a match, but not the word after
答案1
得分: 2
你可以使用以下模式:
(?<=\\bchange my )(\\w+)
第一部分是正向后查找。它确保后面的字符/组在指定模式之前。
第二部分包含了“单词字符”([a-zA-Z0-9_]
)的字符集,放在捕获组中,这样你可以稍后检索它。
https://regex101.com/r/CdKcVh/1
英文:
You can use the following pattern:
(?<=\\bchange my )(\\w+)
The first part is positive lookbehind. It ensures that the following character/group is preceded by the specified pattern.
The second part consists of the character set for "word characters" ([a-zA-Z0-9_]
) within a capture group, so you can retrieve it later.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论