Adam K Dean

Remove params from URL in JavaScript

Published on 3 December 2013 at 12:26 by Adam

Let us say the param we want to remove is session and our URL is http://www.example.com/?session=lasgfnasolgnasgn&id=500&other=100. We can remove it like so:

var oldUrl = "http://www.example.com/?session=lasgfnasolgnasgn&id=500&other=100";
var newUrl = oldUrl.replace(/&?session=([^&]$|[^&]*)/gi, "");
console.log(newUrl);

http://www.example.com/?id=500&other=100

Now let's say we also want to remove other from that URL, we can do that like this:

var oldUrl = "http://www.example.com/?session=lasgfnasolgnasgn&id=500&other=100";
var newUrl = oldUrl.replace(/&?((session)|(other))=([^&]$|[^&]*)/gi, "");
console.log(newUrl);

http://www.example.com/?id=500

Excellent.



This post was first published on 3 December 2013 at 12:26. It was filed under archive with tags regex, javascript.