Return to Snippet

Revision: 52454
at October 30, 2011 10:28 by ultranaut


Updated Code
var q = 'foo=1&blah=a&foo=2&foo=3&blah=b'

function compress(data){
    var q = {}, ret = '';
    data.replace(/([^=&]+)=([^&]*)/g, function(m, key, value){
        q[key] = (q[key] ? q[key] + ',' : '') + value;
    });
    for ( var key in q )
        ret = (ret ? ret + '&' : '') + key + '=' + q[key];
    return ret;
}

console.log(compress(q))

Revision: 52453
at October 24, 2011 05:48 by ultranaut


Initial Code
var q = ['foo', 'bar', 'baz'],
    ret = '';
for (string in q) 
  if (q.hasOwnProperty(string))
    ret = (ret ? ret + "," : "") + string;

console.log(ret);

Initial URL
http://ejohn.org/blog/search-and-dont-replace/

Initial Description
two awesome bits: using ''.replace() perform an action (other than replacing it) on some text, and a simple way to concatenate strings with a separator (retardedly simple but it always confuses me)

Initial Title
Search and don\'t replace

Initial Tags
javascript, replace

Initial Language
JavaScript