-
js parsing URL parameters |
Beta Was this translation helpful? Give feedback.
Answered by
hixb
Jul 19, 2022
Replies: 1 comment
-
const name = getQueryByName('name') console.log(name, age) // fatfish, 100 |
Beta Was this translation helpful? Give feedback.
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
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
`