Skip to content

Instantly share code, notes, and snippets.

@omidnasri
Forked from stinoga/index.js
Created March 13, 2016 08:56
Show Gist options
  • Save omidnasri/ec7cd32aed49608e7a71 to your computer and use it in GitHub Desktop.
Save omidnasri/ec7cd32aed49608e7a71 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment