How to increase each number in a string?

前端 未结 4 702
别跟我提以往
别跟我提以往 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:40

    You can do something like this:

    var str = "sermon[thesis][1][name][2]";
    var newStr = str.replace(new RegExp("\\d+", "g"), function (n) {
        return parseInt(a, 10) + 1;
    });
    

    Basicly, the function would be called with the text been captured by the expression \d+,the text return from the function would be use to replace the captured text.

提交回复
热议问题