Skip to content

Instantly share code, notes, and snippets.

@hcl1687
Created July 14, 2016 00:57
Show Gist options
  • Save hcl1687/16e9be85389b7ff5bb1d1392a77266a4 to your computer and use it in GitHub Desktop.
Save hcl1687/16e9be85389b7ff5bb1d1392a77266a4 to your computer and use it in GitHub Desktop.
parsr a url string to a obj
function parseUrl (url) {
const l = document.createElement('a')
l.href = url
const protocol = l.protocol + '//'
const host = l.hostname
const path = decodeURIComponent(l.pathname)
const reg = /\/(v\d\.\d+)(.*)/g
const match = reg.exec(path)
const ver = match && match[1] || ''
const api = ver ? path.replace('/' + ver, '') : path
return {
protocol,
host,
ver,
api
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment