2016-04-05 49 views
-2

我想寫一個JavaScript正則表達式來匹配其中包含單詞-booker的URL。正則表達式匹配基於單詞的URL

網址是一般般:

http://domain.local/htmlforms/mforms/product1-booker.html 
http://domain.local/htmlforms/mforms/product2-booker.html 
http://domain.local/htmlforms/mforms/product3.html 
http://domain.local/htmlforms/mforms/product4.html 

謝謝。

+1

你有試過什麼嗎?從這裏開始閱讀正則表達式: - http://stackoverflow.com/questions/4736/learning-regular-expressions – rock321987

回答

-1

我認爲這將有助於

/\http:\/\/domain.local\/htmlforms\/mforms\/\w+-booker.html/g 
1

這應做到:

https?\:\/{2}[\w\.]+(\/[\w]+)+\-booker(\/[\w]+|\.[\w]+) 

這將匹配

  • HTTP使用可選的s
  • 兩個正斜槓。
  • 類似物品的單詞,或一次或多次的句號。
  • 正斜槓或詞項一次或多次
  • -booker一次
  • 正斜槓或詞項一次或多次(第二次,櫃面-booker是在中間,則該選項可如果去掉需要)。
  • 句號和類似字符一次或多次的單詞(對於文件名。)

有關示例,參見here

我希望這會有所幫助。

0

另外,請嘗試:

/(?=https?:\/\/[^ ]+-booker[^ ]+)(.*)/g 

(?=https?:\/\/[^ ]*-booker[^ ]+)是一個積極的前瞻 - 斷言這個詞-booker應在網址上找到。

REGEX 101 DEMO