The replaceUrl Thing

stackoverflow question http://stackoverflow.com/questions/27573474/replace-url-parameters-function#27573474

JavaScript

function replaceUrlParam(paramName, paramValue){
    var currentUrl =  'www.mysite.com/index.php?id=15&&cat=20';
    var pattern = new RegExp('('+paramName+'=).*?(&|$)') 
    var newUrl = currentUrl.replace(pattern,'$1' + paramValue + '$2');
    if(newUrl == currentUrl){
        newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue 
    }
    window.history.pushState('',document.title,newUrl);
    alert(newUrl);
    return newUrl;
}



replaceUrlParam('id', 15);