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

yoa*_*avf 1 javascript regex

我有一个看起来像"(3)新东西"的字符串,其中3可以是任何数字.
我想加上或减去这个数字.

我想出了以下方法:

var thenumber = string.match((/\d+/));
thenumber++;
string = string.replace(/\(\d+\)/ ,'('+ thenumber +')');
Run Code Online (Sandbox Code Playgroud)

有更优雅的方式吗?

Gum*_*mbo 5

其他方式:

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