Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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