How can I replace a regex substring match in Javascript?

后端 未结 4 1778
梦如初夏
梦如初夏 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:27

    I think the simplest way to achieve your goal is this:

    var str   = 'asd-0.testing';
    var regex = /(asd-)(\d)(\.\w+)/;
    var anyNumber = 1;
    var res = str.replace(regex, `$1${anyNumber}$3`);
    

提交回复
热议问题