Created
August 23, 2020 20:58
-
-
Save DavidWells/4123e000886b012aa2c7c56411cb1065 to your computer and use it in GitHub Desktop.
Parse URL in browser / node with no dependancies
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 parseUrl(url) { | |
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:([0-9]+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i) | |
return { | |
protocol: match[1] || '', | |
host: match[2] || '', | |
port: match[3] || '', | |
path: match[4] || '', | |
query: match[5] || '', | |
fragment: match[6] || '', | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment