2017-10-09 57 views
-1

我需要匹配(字)匹配兩個單詞之間的一切...... ......(第一:(OR))之間的所有內容

一個正則表達式,其與一個條件

輸入
你可以開車去機場或乘坐出租車,但不要在那裏散步或騎自行車。

比賽
可以either開車前往機場or坐上出租車,但不走路或騎自行車在那裏。

輸出
either開車前往機場or


我試過

(either\s).*(\sor)
但它不會獲得第一或...

a = "You can either drive to the airport or get a taxi but don't walk or bike there." 
 
b = /(either\s).*(\sor)/g.exec(a) 
 
console.log(b[0])

+1

歡迎的StackOverflow! 你到目前爲止嘗試過什麼嗎? StackOverflow不是一個免費的代碼寫入服務,並且期待你 [嘗試先解決你自己的問題](http://meta.stackoverflow.com/questions/261592)。 請更新您的問題,以顯示您已經嘗試過的內容,顯示您在 [最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)中遇到的具體問題。 欲瞭解更多信息,請參閱 [如何問一個好問題], 並採取 [網站之旅](http:// http:// stackoverflow.com/tour) –

回答

1

什麼你要找的是:

/(either\s).+?(\sor)/g

a = "You can either drive to the airport or get a taxi but don't walk or bike there." 
 
b = /(either\s).+?(\sor)/g.exec(a) 
 
console.log(b[0])