javascript - Remove all occurrences of text within string -
say had string in javascript looked this:
var str = "item%5b9%5d.something%5b0%5d.prop1=1&item%5b9%5d.something%5b0%5d.prop2=false&item%5b9%5d.something%5b0%5d.prop3=10%2f04%2f2013+00%3a00%3a00&item%5b9%5d.something%5b1%5d.prop1=2&item%5b9%5d.something%5b1%5d.prop2=false&item%5b9%5d.something%5b1%5d.prop3=10%2f04%2f2013+00%3a00%3a00&item%5b9%5d.something%5b2%5d.prop1=3&item%5b9%5d.something%5b2%5d.prop2=false&item%5b9%5d.something%5b2%5d.prop3=29%2f04%2f2013+00%3a00%3a00&item%5b9%5d.something%5b3%5d.prop1=4&item%5b9%5d.something%5b3%5d.prop2=false&item%5b9%5d.something%5b3%5d.prop3=29%2f04%2f2013+00%3a00%3a00"
and wanted this:
var str = "something%5b0%5d.prop1=1&something%5b0%5d.prop2=false&something%5b0%5d.prop3=10%2f04%2f2013+00%3a00%3a00&something%5b1%5d.prop1=2&something%5b1%5d.prop2=false&something%5b1%5d.prop3=10%2f04%2f2013+00%3a00%3a00&something%5b2%5d.prop1=3&something%5b2%5d.prop2=false&something%5b2%5d.prop3=29%2f04%2f2013+00%3a00%3a00&something%5b3%5d.prop1=4&something%5b3%5d.prop2=false&something%5b3%5d.prop3=29%2f04%2f2013+00%3a00%3a00"
i.e. remove of item%5bx%5d. parts
how go doing this? thought of using like:
str = str.substring(str.indexof('something'), str.length);
but removes first occurrence.
also number in-between %5b , %5d anything, not 9.
this seems should simple reason i'm stumped. found few similarish things on nothing handled above criteria.
you use regular expression :
str = str.replace(/item[^.]+\./g, '');
or if want more precise because you'd want keep item%6b3%4d
:
str = str.replace(/item%5b.%5d\./g, '');
Comments
Post a Comment