Skip to content

Instantly share code, notes, and snippets.

@mirinzhang
Created September 7, 2018 10:40
Show Gist options
  • Save mirinzhang/6635ab10279198f69e40d448f9b958d2 to your computer and use it in GitHub Desktop.
Save mirinzhang/6635ab10279198f69e40d448f9b958d2 to your computer and use it in GitHub Desktop.
1.Parse a query string into an object
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