簡體   English   中英

字符串中的Javascript遞減數

[英]Javascript decrement number in string

如何減少字符串中的數字? 現在我有了這個:

var existingId = "test 10 test";

let newId = existingId .replace(/\d+/g, function(match, number) {
       return parseInt(number)-1;
});

該數字並不總是在同一位置。 我無法將數字存儲在變量中。

但是我的正則表達式不正確。

你差不多了

var existingId = "test 10 test";

let newId = existingId.replace(/\d+/g, function(match) {
    return parseInt(match) - 1;
});

使用匹配參數代替數字

let newId = existingId.replace(/\d+/g, function (match, number) {
                return parseInt(match) - 1;
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM