[英]Javascript decrement number in string
How do I decrement a number in a string? 如何减少字符串中的数字? Right now I've got this:
现在我有了这个:
var existingId = "test 10 test";
let newId = existingId .replace(/\d+/g, function(match, number) {
return parseInt(number)-1;
});
The number is not always in the same place. 该数字并不总是在同一位置。 I cannot store the number in a variable.
我无法将数字存储在变量中。
But my regex is not correct. 但是我的正则表达式不正确。
You almost got it 你差不多了
var existingId = "test 10 test";
let newId = existingId.replace(/\d+/g, function(match) {
return parseInt(match) - 1;
});
use match argument instead number 使用匹配参数代替数字
let newId = existingId.replace(/\d+/g, function (match, number) {
return parseInt(match) - 1;
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.