Skip to content

Instantly share code, notes, and snippets.

@fergalhanley
Last active May 6, 2021 05:02
Show Gist options
  • Save fergalhanley/8743cdda4beee24c37efc5eafe4a90e3 to your computer and use it in GitHub Desktop.
Save fergalhanley/8743cdda4beee24c37efc5eafe4a90e3 to your computer and use it in GitHub Desktop.
Typescript/JS function for parsing a URL and returning the query parameters as an object
export const parseQueryParameters = (url) => {
const parsed = {}
const matches = url.match("\\?([^#]+)")
if (matches && matches.length >= 2) {
const query = matches[1]
query.split('&').forEach((params) => {
const frag = params.split('=')
parsed[frag[0]] = frag[1]
})
}
return parsed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment