简体   繁体   English

正则表达式在特定单词之后匹配一个单词

[英]regex match a word after a particular word

I created a regex for matching a string which could be possibly something like shown below: 我创建了一个正则表达式来匹配一个字符串,它可能类似于以下所示:

then "hello I am here" blah blah

or 要么

then hello blah blah

I am trying to match the word just after then . then我正试图匹配这个词。 For above strings the expected outputs are 对于上述字符串,预期输出为

"hello I am here"   //if the words are in quotes

or 要么

hello

I tried this regex &quot;(\\w*[\\s]*)*&quot;|(?<=\\bthen\\s)(\\w+) , which is working fine online but showing error in firefox. 我尝试了此正则表达式&quot;(\\w*[\\s]*)*&quot;|(?<=\\bthen\\s)(\\w+) ,该函数在网上正常运行,但在firefox中显示错误。 Error 错误

在此处输入图片说明

Try 尝试

var regex = /then\s*(&quot;(.*?)&quot;|(\w*))\s*.*/

function getSomething(string){
    var arr = regex.exec(string);
    console.log(arr);
    return arr.length > 1 ? arr[1] : undefined;
}

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM