Skip to content

@kflorence /parseUri.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Modified version of Steven Levithan's parseUri 1.2.2
// 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
Something went wrong with that request. Please try again.