I want to replace ":id" in url string with particular id e.g. 54 My question would be is it enough to use "/(:id)/i" for replacing this parameter, or I should use something more complex? Thanks
var url = "http://www.test.com/api/person/:id/details"
var str = url.replace(/(:id)/i, 54);
Updated:
I just realized that it would be great to have a function which would be able to replace multiple values in URL. Example:
function replaceParametersInUrl(url, params) {
var regex = new RegExp(Object.keys(params).join('|'), 'gi');
return url.replace(regex, function(matched) {
return params[matched];
});
}
console.log(replaceParametersInUrl("api/person/:userId/items/:itemId", {userId: 56, itemId: 1}));
The problem with this solution is that it replaces just userId and itemId strings without ":". How I can achieve that it would replace those strings including ":"?
/:id/would be enough.