繁体   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