3

I need help to match a url that matches only if

  • the path matches exactly /gummybear/ or /gummybear (case sensetive)
  • the protocol is http or https
  • the domain/host/port/hash can be anything

the regex should not match if

  • the path is /gummybear/foobar
  • it contains search parameters such as ?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
2
  • What are the differences between your "should match" https://example.com:81/gummybear/#/exaple/1234/ and your "shouldn't match" the path is /gummybear/foobar ? Commented Oct 13, 2015 at 23:49
  • @FedericoPiazza it matches another sub directory Commented Oct 14, 2015 at 0:04

1 Answer 1

9

For your specific needs, I can come up with this regex:

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

Regular expression visualization

Working demo

enter image description here

I haven't escaped slashes to make it more readable, but for javascript you can use:

^https?:\/\/[^\/]+\/gummybear(?:\/?|\/?#.*)$
Sign up to request clarification or add additional context in comments.

2 Comments

what tool did you use to generate that diagram? is it on regex101? where?
@SkeetsO'Reilly I used Debuggex Demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.