如何增加字符串中的每个数字?

Dzi*_*mid 2 javascript regex

我需要在字符串中搜索其中的任何数字并将数字增加1.这样'sermon[thesis][1][name][2]'就变成了'sermon[thesis][2][name][3]'.

Mac*_*Mac 5

这样就可以了:

"sermon[thesis][1][name][2]".replace(/\[(\d+)\]/g, function(match, number) {
    return "[" + (Number(number) + 1) + "]";
});
Run Code Online (Sandbox Code Playgroud)

工作演示:jsFiddle.

编辑:

要增加最后一个数字,你可以在最后一个数字$之前添加一个美元符号/,这是一个demo:jsFiddle.