解析 URL
function parseUrl(url) {
let schemeStr = '(?:([^/?#]+))?//(?:([^:]*)(?::?(.*))@)?',
urlStr = '(?:([^/?#:]*):?([0-9]+)?)?([^?#]*)(\\?(?:[^#]*))?',
fragmentStr = '(#(?:.*))'
let pattern = RegExp(`^${schemeStr}${urlStr}${fragmentStr}?`)
let matched = url.match(pattern) || []
return {
protocol: matched[1],
username: matched[2],
password: matched[3],
hostname: matched[4],
port: matched[5],
pathname: matched[6],
search: matched[7],
hash: matched[8],
}
}
function parseUrl(url) {
const urlObj = new URL(url)
return {
protocol: urlObj.protocol,
username: urlObj.username,
password: urlObj.password,
hostname: urlObj.hostname,
port: urlObj.port,
pathname: urlObj.pathname,
search: urlObj.search,
hash: urlObj.hash
}
}