Last active
June 7, 2021 11:08
-
-
Save simonsmith/5152680 to your computer and use it in GitHub Desktop.
Convert 'name=true&test=hello' into an object. Taken from http://stevenbenner.com/2010/03/javascript-regex-trick-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
function deparam(str) { | |
var o = {}; | |
var reg = /\\?([^?=&]+)(=([^&#]*))?/g; | |
str.replace(reg, function($0, $1, $2, $3) { | |
if (typeof $3 == 'string') { | |
o[decodeURIComponent($1)] = decodeURIComponent($3); | |
} | |
}); | |
return o; | |
} | |
// Use | |
deparam('name=simon') | |
// Returns | |
{ | |
name: 'simon' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment