Created
August 2, 2018 23:00
-
-
Save elvismdev/ee549ba8a738c6e6cf39f9339d9e8525 to your computer and use it in GitHub Desktop.
Replaces a parameter value from a given URL query string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceUrlParam(url, paramName, paramValue) { | |
if (paramValue == null) { | |
paramValue = ''; | |
} | |
var pattern = new RegExp('\\b('+paramName+'=).*?(&|#|$)'); | |
if (url.search(pattern)>=0) { | |
return url.replace(pattern,'$1' + paramValue + '$2'); | |
} | |
url = url.replace(/[?#]$/,''); | |
return url + (url.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment