I have three cases:
1) when the string looks like this /televisions?3dtv=true – I need to remove ?3dtv=true from it
2) when the string looks like this /televisions?3dtv=true&brand=panasonic – I need to remove &brand=panasonic from it
3) when the string looks like this /televisions?3dtv=true&brand=panasonic&led=true – I need to remove everything after last & including & so – &led=true
I guess number 2 and 3 are the same.
Any help would be appreciated.
Unfortunately,
lastIndexOfis only supported in IE9, so alternatively you can consider regular expressions:I updated the answer accordingly to the new requirements. The expression will either replace everything from the last
&on or, if there is none, from?. As the alternation is evaluated from left to right, it is guaranteed that&...is replaced if it is present.Expression explained: