Skip to content

Instantly share code, notes, and snippets.

@altmind
Created April 2, 2014 06:52
Show Gist options
  • Save altmind/9929101 to your computer and use it in GitHub Desktop.
Save altmind/9929101 to your computer and use it in GitHub Desktop.
function parseUrl(url) {
if ($.type(url) === "object") {
return url;
}
var matches = /^(((([^:\/#\?]+:)?(?:\/\/((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?]+)(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/.exec(url);
return matches ? {
href:matches[0] || "",
hrefNoHash:matches[1] || "",
hrefNoSearch:matches[2] || "",
domain:matches[3] || "",
protocol:matches[4] || "",
authority:matches[5] || "",
username:matches[7] || "",
password:matches[8] || "",
host:matches[9] || "",
hostname:matches[10] || "",
port:matches[11] || "",
pathname:matches[12] || "",
directory:matches[13] || "",
filename:matches[14] || "",
search:matches[15] || "",
hash:matches[16] || ""
} : {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment