I am struggling trying to create a really simple RegExp. (probably lacking some hours of sleep).
I just have has an input this string : /users/10/contracts/1 My regex is this one : /users/(\w+)/contracts/(\w+)/
I want to replace all matches with this kind of string : /users/{variable1}/contracts/{variable2}
Here is my complete source code :
var customRegex = "/users/(\\w+)/contracts/(\\w+)";
var i =0;
var finalUrl = "/users/1234/contracts/5678".replace(new RegExp(customRegex, 'gi'), function myFunction(x, y){
return "{variable" + i + "}";
});
console.log(finalUrl);
Could you please help me?
I wish you a nice day and thank you for your help.
String#replacewith a counter.var finalUrl = "/users/1234/contracts/5678"; var i = 1; finalUrl = finalUrl.replace(/\d+/g, function () { return "{variable" + i++ + "}"; }); console.log(finalUrl);