Created
September 7, 2018 10:40
-
-
Save mirinzhang/6635ab10279198f69e40d448f9b958d2 to your computer and use it in GitHub Desktop.
1.Parse a query string into an object
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 urlQuery = { | |
getAll(url) { | |
if(!url) { | |
return {}; | |
} | |
const regx = /([^?=&]+)=([^&]*)?/g, | |
result = {}; | |
url.replace(regx, (_, $1, $2) => result[$1] = decodeURIComponent($2 || '')); | |
return result; | |
}, | |
getByName(url, name) { | |
if(!url) { | |
return null; | |
} | |
const result = url.match(new RegExp('(\\?|&)' + name + '=(.*?)(&|$)')), | |
param = (result && decodeURIComponent(result[2])) || null; | |
return param; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment