gpt4 book ai didi

JavaScript:从字符串中的数字添加或减去

转载 作者:行者123 更新时间:2023-11-30 07:07:00 25 4
gpt4 key购买 nike

我有一个字符串,看起来像“(3) New stuff”,其中 3 可以是任何数字。
我想加上或减去这个数字。

我想到了以下方法:

var thenumber = string.match((/\d+/));
thenumber++;
string = string.replace(/\(\d+\)/ ,'('+ thenumber +')');

有没有更优雅的方法呢?

最佳答案

另一种方式:

string = string.replace(/\((\d+)\)/ , function($0, $1) { return "(" + (parseInt($1, 10) + 1) + ")"; });

关于JavaScript:从字符串中的数字添加或减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/504219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com