Skip to content

Instantly share code, notes, and snippets.

@stinoga
Created December 23, 2013 18:05
Show Gist options
  • Save stinoga/8101816 to your computer and use it in GitHub Desktop.
Save stinoga/8101816 to your computer and use it in GitHub Desktop.
Replacing query string parameter values.
// Update the appropriate href query string parameter
function paramReplace(name, string, value) {
// Find the param with regex
// Grab the first character in the returned string (should be ? or &)
// Replace our href string with our new value, passing on the name and delimeter
var re = new RegExp("[\\?&]" + name + "=([^&#]*)"),
delimeter = re.exec(string)[0].charAt(0),
newString = string.replace(re, delimeter + name + "=" + value);
return newString;
}
@Mike-Savin
Copy link

To replace all the query parameters with given value you can use something like this:

url.replace(/([?&])(((?![?&#=]).)*=)((?![?&#=]).)*/g, '$1$2xxx');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment