Created
December 29, 2013 05:55
-
-
Save stuartpb/8167919 to your computer and use it in GitHub Desktop.
A small function to parse URLs without relying on the DOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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