3 ms·
That doesn't handle strings like foo=1&foo=2&foo=3&blah=a&blah=b&foo=4 correctly. You'd expect foo=1,2,3,4&blah=a,b but get foo=1,2,3&blah=a,b&
by chmod775 6y ago
That doesn't handle strings like
foo=1&foo=2&foo=3&blah=a&blah=b&foo=4
correctly. You'd expect
foo=1,2,3,4&blah=a,b
but get
foo=1,2,3&blah=a,b&foo=4
I don't think it's possible to solve this just using a single search/replace.
- recursive 6y agoIt wasn't clear from the post whether that was a valid input. If it is, some parts would need to be re-ordered, making it a very fancy search/replace indeed. But I still think it's possible. One or two replacements to switch the ordering, and then my original one to get the final output.
- recursive 6y agoI put some effort into trying to find a search/replace to do it. Without a high-powered replacement function, I couldn't do it. But I did do this instead. function compress(data){ return data.split`&`.sort().join`&`.replace(/(?<=(\w+)=[^&]*)&\1=/g,','); }
- chmod775 6y agoThat's a really creative way to do it!