Javascript RegExp: replace parameter value in query string

Task: change the value of parameter 'tid' in given querystring.
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);


Popular posts from this blog

Data visualization for Roadmap Microsite (Angular and D3.js)

Conceptual design

Four tools for prototyping: Sketch, Figma, Principle, and FramerX