How to increase each number in a string?

前端 未结 4 693
别跟我提以往
别跟我提以往 2020-12-11 10:53

I need to search a string for any numbers in it and increase the numbers by 1. So that this \'sermon[thesis][1][name][2]\' becomes \'sermon[thesis][2][nam

4条回答
  •  庸人自扰
    2020-12-11 11:49

    You can use replace, it can actually take a function as the replacement "string".

    var str = 'sermon[thesis][1][name][2]';
    str = str.replace(/(\d+)/g, function(a){
       return parseInt(a,10) + 1;
    });
    console.log(str); //'sermon[thesis][2][name][3]'
    

提交回复
热议问题