1

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?

1
  • there are many ways, but regular expressions will be your best choice. just write a function that does that using regular expressions. should be very few lines of code.. Commented Mar 12, 2012 at 10:11

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is working good but I think there should be a small fix. If a param is repeated then it removes only one. For Example: example.com/index?cat10&change=1&change=0 is converted to example.com/index?cat10&change=0
1

Here jsfiddle example of a function that does that: http://jsfiddle.net/ANGsJ/1/
PS. I used the regex of Just_Mad, since mine was a bit uglier ;)

4 Comments

It fails on process('/browse/102?color=red&type=car&mode=2&rpp=10&ajax=1&change=1', ["mode"]);
What do you mean by it fails? Here you have an updated version. Just_Mad's regex removes too many characters (especially & and ?). Mine will not remove them at all, but it still should be a correct url: jsfiddle.net/ANGsJ/2 . If you have problems you can always adjust the regex.
And here you have a quick solution if you really want to have pretty urls: jsfiddle.net/ANGsJ/4
Well, we just have a bit different view of the correct result :)
1

Here is a simple utility I use in my application to play around with URL. http://jsfiddle.net/gwB2C/

Simple to use:

var url_parser = new URLParser('/browse/102?color=red&type=car&rpp=10&ajax=1&change=1');
alert(url_parser.toString()); 
// result = "/browse/102?color=red&type=car&rpp=10&ajax=1&change=1"
url_parser.removeParams(["color", "type"]);
alert(url_parser.toString()); 
// result = "/browse/102?rpp=10&ajax=1&change=1"
url_parser.addParams({color:"green", test : 1});
alert(url_parser.toString()); 
// result = "/browse/102?rpp=10&ajax=1&change=1&color=green&test=1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.