簡體   English   中英

需要一個javascript正則表達式來匹配特定的路徑

[英]Need a javascript regex to match a specific path

我需要幫助才能匹配僅匹配的網址

  • 路徑完全匹配/ gummybear /或/ gummybear(case casetive)
  • 協議是http或https
  • 域/主機/端口/哈希可以是任何東西

如果正則表達式匹配

  • 路徑是/ gummybear / foobar
  • 它包含搜索參數,例如?query = string

到目前為止我得到了這個:

/^http[s]?:\/\/?[^\/\s]+\/gummybear[\/]?/

它應該是真實的例子

https://www.example.com:81/gummybear/
http://www.example.com/gummybear#top
https://example.com:81/gummybear#/foobar/?search=params
http://www.exa.mple.com:81/gummybear
https://example.com:81/gummybear/#/exaple/1234/

這個例子應該是假的

https://www.example.com:81/foo/gummybear/
https://www.example.com:81/guMmybear/
https://www.example.com:81/gummybear.html
http://www.example.com/gummybear/apple#top
file://example.com:81/gummybear#/foobar/?search=params
http://www.exa.mple.com:81/gummybear?search=apple#lol
https://example.com:81/#/gummybear/
http://www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top

根據您的具體需求,我可以拿出這個正則表達式:

^https?://[^/]+/gummybear(?:/?|/?#.*)$

正則表達式可視化

工作演示

在此輸入圖像描述

我沒有轉義斜線以使其更具可讀性,但對於javascript,您可以使用:

^https?:\/\/[^\/]+\/gummybear(?:\/?|\/?#.*)$

暫無
暫無

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

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