How can I replace a regex substring match in Javascript?

var str="asd-0.testing";
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);

Or if you’re sure there won’t be any other digits in the string:

var str="asd-0.testing";
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);

Leave a Comment