Skip to content

Instantly share code, notes, and snippets.

@StuPig
Created June 19, 2014 13:49
Show Gist options
  • Save StuPig/8c9ea0ebd6caa90d8097 to your computer and use it in GitHub Desktop.
Save StuPig/8c9ea0ebd6caa90d8097 to your computer and use it in GitHub Desktop.
parse URI to URI object like location object。URI转换,secheme转换
function parseURI (uri) {
"use strict";
if (!uri) return;
var regexMaybe = function (str) {
return '(?:' + str + ')?';
},
reg = new RegExp('^' + regexMaybe('(([^:]+):)?') + '\\/\\/([^:/]+)/*' + regexMaybe(':(\\d+)\\/*') + regexMaybe('([^?#]+)') + regexMaybe('\\?([^#]+)') + regexMaybe('#(\\w+)')),
match = uri.match(reg),
protocol = match[1] || '',
hostname = match[3] || '',
port = match[4] || '',
host = hostname + ':' + port,
origin = protocol + '//' + host,
pathname = match[5] || '',
search = match[6] || '',
hash = match[7] || '';
return {
protocol: protocol,
hostname: hostname,
port: port,
host: host,
origin: origin,
pathname: pathname,
href: uri,
search: search,
hash: hash
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment