How can I replace a regex substring match in Javascript?

后端 未结 4 1779
梦如初夏
梦如初夏 2020-11-29 21:06
var str   = \'asd-0.testing\';
var regex = /asd-(\\d)\\.\\w+/;

str.replace(regex, 1);

That replaces the entire string str with

4条回答
  •  半阙折子戏
    2020-11-29 21:20

    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);
    

提交回复
热议问题