如何增加字符串中的每个数字? [英] How to increase each number in a string?

查看:140
本文介绍了如何增加字符串中的每个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在字符串中搜索其中的任何数字并将数字增加1.这样'布道[论文] [1] [名称] [2]'变成'布道[论文] [2] [姓名] [3]'

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][name][3]'.

推荐答案

这样做可以解决问题:

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

工作演示: jsFiddle

编辑:

要增加最后一个数字,您需要在最后一个 / 之前添加一个美元符号 $ ,这是一个演示: jsFiddle

To increment the last number, you would add a dollar sign $ before the last /, here's a demo: jsFiddle.

这篇关于如何增加字符串中的每个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆