I want to remove some params from a URL string:
For Example:
I have a string like this:
var url = '/browse/102?color=red&type=car&rpp=10&ajax=1&change=1';
and an array like this:
var ignore = new Array("change","ajax");
Result:
/browse/102?color=red&type=car&rpp=10
What is the shortest and quickest way to achieve this?
What about a RegExp? Here is an example function:
var url = '/browse/102?color=red&type=car&rpp=10&a=mode&ajax=1&change=1&mode&test';
var ignore = new Array("change","ajax", "mode", "test");
alert(removeParams(url, ignore));
function removeParams(address, params)
{
for (var i = 0; i < params.length; i++)
{
var reg = new RegExp("([\&\?]+)(" + params[i] + "(=[^&]+)?&?)", "g")
address = address.replace(reg, function($0, $1) { return $1; });
}
return address.replace(/[&?]$/, "");
}
Edit: Moved to a separate function like Michal B. did.