Created
June 19, 2014 13:49
-
-
Save StuPig/8c9ea0ebd6caa90d8097 to your computer and use it in GitHub Desktop.
parse URI to URI object like location object。URI转换,secheme转换
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 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