Javascript regex and replacing consecutive character sets -
i'm having problem replacing unnecessary character sets string. following regex replaces first occurrence of character set "&=&" doesn't take care of leftovers. how 1 replace first , consecutive occurences of character set regex?
var test = "var1=data1&var2=data2&=&=&=&=&var3=data3&=&=&var4=data4&var5=&var6=data6"; var result = ""; result = test.replace(/(\&\=\&)+/g, '&'); // result returns "var1=data1&var2=data2&=&=&var3=data3&=&var4=data4&var5=&var6=data6"
you can use positive look-ahead instead. basically, &=
, followed &
:
var = "var1=data1&var2=data2&=&=&=&=&var3=data3&=&=&var4=data4&var5=&var6=data6"; var b = a.replace(/\&=(?=\&)/g,''); // b = var1=data1&var2=data2&var3=data3&var4=data4&var5=&var6=data6
(which think you're after)
Comments
Post a Comment