Created
August 24, 2011 23:16
-
-
Save kflorence/1169555 to your computer and use it in GitHub Desktop.
Modified version of Steven Levithan's parseUri 1.2.2
This file contains hidden or 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
// Modified version of Steven Levithan's parseUri 1.2.2 | |
// See: http://blog.stevenlevithan.com/archives/parseuri | |
function parseUri( uri ) { | |
var uriRegex = new RegExp( | |
// Protocol | |
"^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?" + | |
// Authority | |
"(" + | |
// Credentials | |
"(?:(" + | |
// Username | |
"([^:@]*)" + | |
// Password | |
"(?::([^:@]*))?" + | |
")?@)?" + | |
// Host | |
"([^:/?#]*)" + | |
// Port | |
"(?::(\\d*))?" + | |
// Relative | |
")(" + | |
// Path | |
"(" + | |
// Directory | |
"(/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?" + | |
// File | |
"([^?#/]*)" + | |
")" + | |
// Query | |
"(?:\\?([^#]*))?" + | |
// Anchor | |
"(?:#(.*))?" + | |
")" | |
), | |
// Parses key/value pairs from a query string | |
queryKeysRegex = /(?:^|&)([^&=]*)=?([^&]*)/g, | |
// Keys for the uri hash, mapped from the matches array | |
properties = [ | |
"source", "protocol", | |
"authority", "credentials", | |
"username", "password", | |
"host", "port", | |
"relative", "path", | |
"directory", "file", | |
"extension", "query", | |
"anchor" | |
], | |
i = 0, | |
l = properties.length, | |
matches = uriRegex.exec( uri ), | |
uri = { | |
queryData: {} | |
}; | |
for ( ; i < l; i++ ) { | |
uri[ properties[ i ] ] = matches[ i ] || ""; | |
} | |
uri.query.replace( queryKeysRegex, function( matched, key, value, offset, str ) { | |
if ( key && key.length ) { | |
uri.queryData[ key ] = value; | |
} | |
}); | |
return uri; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unfortunately this doesnt correctly parse:
http://localhost:1111/?q=1
"q=1" shows up as "extension" but should be the query / queryData