Thursday, February 6, 2014

Change URL parameter value of a variable in a Javascript Regular Expression (RegEx)

function changeParamByName(href, paramName, newVal) {
var tmpRegex = new RegExp("(" + paramName + "=)[a-z]+", 'ig');
return href.replace(tmpRegex, '$1'+newVal);
}
 
var href = changeParamByName("http://domain.com?var=thisIsOld&bca=something&b=2&c=3","bca","theNewValue");
console.log(href);



Reference:

http://blog.adrianlawley.com/jquery-change-url-parameter-value/

1 comment: