Javascript RegExp: replace parameter value in query string
Task: change the value of parameter 'tid' in given querystring.
Original querystring:
New 'tid' parameter value:
You can do it using 'replace' function. Final code to replace:
But if you need just extract 'tid' value use 'exec':
Original querystring:
var linkParam = 'tid=8&id=11008&location=news';
New 'tid' parameter value:
var tagId = 12;
You can do it using 'replace' function. Final code to replace:
var tagIdReg = /[tid=](\d+)[&]/i;
document.write(linkParam.replace(tagIdReg, '='+tagId+'&'));
But if you need just extract 'tid' value use 'exec':
var tagIdReg = /[tid=](\d+)[&]/i;
var regResult = tagIdReg.exec(linkParam);
document.write(regResult[1]);
Comments
Post a Comment