Use string.replacewith function as the second argument:
var s1 = "This is a string with numbers 1 2 3 4 5 6 7 8 9 10";
var s2 = s1.replace(/\d+/g, function(x) { return Number(x)+1; });
s2;
Note that if you use comparable groups, the first argument to the function will be a complete match, and each subsequent argument will be a numbered matching group.
var x = "This is x1, x2, x3.";
var y = x.replace(/x(\d+)/g, function(m, g1) {
return "y" + (Number(g1)+1);
});
y;
source
share