-
-
Save wilcorrea/b47ed61e19069575f0e18c88c8e30723 to your computer and use it in GitHub Desktop.
Recupera parâmetro de uma querystring
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
const requestQuery = request => { | |
const param = request.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]") | |
const regex = new RegExp("[\\?&]" + param + "=([^&#]*)") | |
const results = regex.exec(location.search); | |
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")) | |
} | |
// caso de uso | |
// http://endereco.com.br?param1=123¶m2=456¶m3=789 | |
console.log(requestQuery('param1')) // 123 | |
console.log(requestQuery('param2')) // 456 | |
console.log(requestQuery('param3')) // 789 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment