簡體   English   中英

正則表達式在特定單詞之后匹配一個單詞

[英]regex match a word after a particular word

我創建了一個正則表達式來匹配一個字符串,它可能類似於以下所示:

then "hello I am here" blah blah

要么

then hello blah blah

then我正試圖匹配這個詞。 對於上述字符串,預期輸出為

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

要么

hello

我嘗試了此正則表達式&quot;(\\w*[\\s]*)*&quot;|(?<=\\bthen\\s)(\\w+) ,該函數在網上正常運行,但在firefox中顯示錯誤。 錯誤

在此處輸入圖片說明

嘗試

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;
}

演示: 小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM