parseQueryString2

by sanford

JavaScript

String.implement('parseQueryString2',
    function(decodeKeys,decodeValues) {
		if (decodeKeys == null) decodeKeys = true;
		if (decodeValues == null) decodeValues = true;                     
        var obj={}; 
                     
                     
        this.replace(new RegExp('(?:^|&|;)(\\w+)(?:\\[\\])?(=?)([^&;]*)','g'), function(chunk,key,set,value){ 
           // console.log(arguments);
    if (decodeKeys) key = decodeURIComponent(key);
    if (decodeValues) value = decodeURIComponent(value);

    var existingKey = obj[key];

    if (typeOf(existingKey) == 'array') 
        existingKey.push(value);
	else 
        obj[key] = existingKey != null ? [existingKey, value] : value;
       
}); 
        return obj;
    })

testQS="a=99_33&bb&d=3;sd2&ff[]=val%20not&gg=bool&ff=anotherval&ss&zz=&yy";
//console.log(testQS.parseQueryString().ff);
console.log(testQS.parseQueryString2());