remove all empty values from url

javascript, preg-replace, regex

Solution

Something like this:

s = s.replace(/[^=&]+=(&|$)/g,"").replace(/&$/,"");

That is, remove groups of one or more non-equals/non-ampersand characters that are followed by an equals sign and ampersand or end of string. Then remove any leftover trailing ampersand.

Demo: http://jsfiddle.net/pKHzr/

Problem

I want to remove all empty values from an url: ``` var s="value1=a&value2=&value3=b&value4=c&value5="; s = s.replace(...???...); alert(s); ``` Expected output: ``` value1=a&value3=b&value4=c ``` I only need the query part of the URL to be taken into account.

Original source