Tags: #regex
Pattern…
https?:\/\/(?:www\.)?[\w\.]{2,256}\/?(?:[\w/]+)?(?:\?[^#]+)?(?:#.+)?$
Note: do not use, it’s not going to cover all use cases (as matching a uri properly is a complex thing to do).
Matches…
GOOD...
http://example.com
https://example.com
http://example.com/
https://example.com/
# This might be an issue (as it matches any subdomain currently)...
http://whatever.example.com
https://whatever.example.com
http://www.example.com
https://www.example.com
http://www.example.com/
https://www.example.com/
http://example.com/foo/bar
https://example.com/foo/bar
https://example.com/foo/bar/
http://www.example.com/foo/bar
https://www.example.com/foo/bar
https://www.example.com/foo/bar/
http://example.com?foo=bar#beepboop
https://example.com?foo=bar#beepboop
http://example.com/?foo=bar#beepboop
https://example.com/?foo=bar#beepboop
http://www.example.com?foo=bar#beepboop
https://www.example.com?foo=bar#beepboop
http://www.example.com/?foo=bar#beepboop
https://www.example.com/?foo=bar#beepboop
http://example.com/foo/bar?foo=bar#beepboop
https://example.com/foo/bar?foo=bar#beepboop
https://example.com/foo/bar/?foo=bar#beepboop
http://www.example.com/foo/bar?foo=bar#beepboop
https://www.example.com/foo/bar?foo=bar#beepboop
https://www.example.com/foo/bar/?foo=bar#beepboop
http://example.com#beepboop
https://example.com#beepboop
http://example.com/#beepboop
https://example.com/#beepboop
http://www.example.com#beepboop
https://www.example.com#beepboop
http://www.example.com/#beepboop
https://www.example.com/#beepboop
http://example.com/foo/bar#beepboop
https://example.com/foo/bar#beepboop
https://example.com/foo/bar/#beepboop
http://www.example.com/foo/bar#beepboop
https://www.example.com/foo/bar#beepboop
https://www.example.com/foo/bar/#beepboop
BAD...
https://example.com:8080
https://example.com:8080/
Anything other than ^^