Skip to content

Instantly share code, notes, and snippets.

@bekapod
Last active November 28, 2016 11:03
Show Gist options
  • Save bekapod/7a52164ef34a5e072e65 to your computer and use it in GitHub Desktop.
Save bekapod/7a52164ef34a5e072e65 to your computer and use it in GitHub Desktop.
JS: Replace URL Parameter
function replaceUrlParam(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"),
separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
} else {
return uri + separator + key + "=" + value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment