Skip to content
Discussion options

You must be logged in to vote

js parsing URL parameters

const getQueryByName = (name) => { const queryNameRegex = new RegExp([?&]${name}=([^&]*)(&|$)`)
const queryNameMatch = window.location.search.match(queryNameRegex)
// Generally, it will be decoded by decodeURIComponent
return queryNameMatch ? decodeURIComponent(queryNameMatch[1]) : ''
}

const name = getQueryByName('name')
const age = getQueryByName('age')

console.log(name, age) // fatfish, 100
`

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by xiao-ice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants