Created
July 14, 2016 00:57
-
-
Save hcl1687/16e9be85389b7ff5bb1d1392a77266a4 to your computer and use it in GitHub Desktop.
parsr a url string to a obj
This file contains hidden or 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 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