简体   繁体   English

需要一个javascript正则表达式来匹配特定的路径

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

I need help to match a url that matches only if 我需要帮助才能匹配仅匹配的网址

  • the path matches exactly /gummybear/ or /gummybear (case sensetive) 路径完全匹配/ gummybear /或/ gummybear(case casetive)
  • the protocol is http or https 协议是http或https
  • the domain/host/port/hash can be anything 域/主机/端口/哈希可以是任何东西

the regex should not match if 如果正则表达式匹配

  • the path is /gummybear/foobar 路径是/ gummybear / foobar
  • it contains search parameters such as ?query=string 它包含搜索参数,例如?query = string

So far i got this: 到目前为止我得到了这个:

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

examples it should be true for 它应该是真实的例子

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/

examples that it should be false for 这个例子应该是假的

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

For your specific needs, I can come up with this regex: 根据您的具体需求,我可以拿出这个正则表达式:

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

正则表达式可视化

Working demo 工作演示

在此输入图像描述

I haven't escaped slashes to make it more readable, but for javascript you can use: 我没有转义斜线以使其更具可读性,但对于javascript,您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM