Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created August 23, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/4123e000886b012aa2c7c56411cb1065 to your computer and use it in GitHub Desktop.
Save DavidWells/4123e000886b012aa2c7c56411cb1065 to your computer and use it in GitHub Desktop.
Parse URL in browser / node with no dependancies
function parseUrl(url) {
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:([0-9]+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i)
return {
protocol: match[1] || '',
host: match[2] || '',
port: match[3] || '',
path: match[4] || '',
query: match[5] || '',
fragment: match[6] || '',
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment