Skip to content

Instantly share code, notes, and snippets.

@tobsn
Created September 14, 2012 03:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobsn/3719623 to your computer and use it in GitHub Desktop.
Save tobsn/3719623 to your computer and use it in GitHub Desktop.
node.js parse array form from query string into array like php does
url.query = function( url ) {
var parsed = this.parse( url, true, false ),
query = Object.keys( parsed.query ),
result = {};
if( query.length > 0 ) {
query.forEach(function(key){
if( key.match( /([^\[]+)\[([^\]]+)\]/g ) ) {
key.replace( /([^\[]+)\[([^\]]+)\]/g, function( $0, $1, $2 ) {
result[$1] = result[$1] || {};
result[$1][$2] = parsed.query[key];
});
}
else {
result[key] = parsed.query[key];
}
});
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment