Skip to content

Instantly share code, notes, and snippets.

@akmandev
Created January 10, 2017 16:23
Show Gist options
  • Save akmandev/a36c21cc32ba66932bd231997a3bde98 to your computer and use it in GitHub Desktop.
Save akmandev/a36c21cc32ba66932bd231997a3bde98 to your computer and use it in GitHub Desktop.
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
@zzbo
Copy link

zzbo commented Apr 19, 2019

how about URL with hash?

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