To answer your real real question:
for (param in queryParamsToRemove){
queryString = queryString.replace(new RegExp('[&\?]' + queryParamsToRemove[param] + '=.*(&?)', 'g'), '\1');
}
That should work.
Otherwise you could use a function like this one: The $.param( ) inverse function in JavaScript / jQuery (Simply remove jQuery
from function name and you can use it without any library).
Something like this:
unparam = function (value) {
var
// Object that holds names => values.
params = [],
// Get query string pieces (separated by &)
pieces = value.split('&'),
// Temporary variables used in loop.
pair, i, l;
// Loop through query string pieces and assign params.
for (i = 0, l = pieces.length; i < l; i++) {
pair = pieces[i].split('=', 2);
// Repeated parameters with the same name are overwritten. Parameters
// with no value get set to boolean true.
params[decodeURIComponent(pair[0])] = (pair.length == 2 ?
decodeURIComponent(pair[1].replace(/\+/g, ' ')) : true);
}
return params;
};
var query = unparam(querystr);
for(var i in queryParamsToRemove) {
delete query[queryParamsToRemove[i]];
}
Turning the query string into an array, and from there you can remove your values and convert it back to a query string. If you use jQuery you can use jQuery.param()
Otherwise it shouldn't be too hard to make your own function.
To now convert the query array into a string we could do:
reparam = function (queryArray) {
var res = [];
for(var i in queryArray) {
res.push(i + "=" + queryArray[i]);
}
return res.join('&');
}
var queryString = reparam(query);