Skip to content

Instantly share code, notes, and snippets.

@zadeviggers
Created May 20, 2023 04:28
Show Gist options
  • Save zadeviggers/9265a75a19ea2761d8c1035e51fe7aad to your computer and use it in GitHub Desktop.
Save zadeviggers/9265a75a19ea2761d8c1035e51fe7aad to your computer and use it in GitHub Desktop.

Parses any well-formed URI (and makes safe asumptions for commonly used bad formations), and outputs all the useful information in capture groups:

^(?:(.+?):\/\/)?(?:([^@/\n]+)@)?((?:[a-z]|[0-9]|\.)+)(?:\:([0-9]{1,5}))?([^#?\n]+)?(\?[^#\n]+)?(?:#(.+))?$

Tests:

http://beans.com@google.com/@page/page.html?query&q=very true#fragment
http://beans.com/bees@google.com/@page/page.html?query&q=very true#fragment
http://beans.com@google.com:1000/@page/page.html?query&q=very true#fragment
http://google.com:1000/@page/page.html?query&q=very true#fragment
http://google.com/@page/page.html?query&q=very true#fragment
http://127.0.0.1/@page/page.html?query&q=very true#fragment
http://127.0.0.1:5555/@page/page.html?query&q=very true#fragment
http://username@127.0.0.1:5555/@page/page.html?query&q=very true#fragment
http://localhost/
http://localhost/#fragment
http://localhost:4545/
http://localhost:1234/beans@localhost:4545/?query&q=very true#fragment
http://localhost:1234@localhost:4545/?query&q=very true#fragment
google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment