Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Created December 29, 2013 05:55
Show Gist options
  • Save stuartpb/8167919 to your computer and use it in GitHub Desktop.
Save stuartpb/8167919 to your computer and use it in GitHub Desktop.
A small function to parse URLs without relying on the DOM
function parseUrl(url) {
var urlRegex = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result = urlRegex.exec(url);
return {
href: result[0], protocol: result[1], slashes: result[2],
hostname: result[3], port: result[4], pathname: result[5],
query: result[6], hash: result[7]};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment