将url末尾的数字替换为递增的值

replace number at the end of url with incremented value

本文关键字:替换 数字 url      更新时间:2023-09-26

我需要用一个递增的值匹配并替换url末尾的数字。

url = "http://127.0.0.1:8000/hello/abc/14/"

输出
result = "http://127.0.0.1:8000/hello/abc/15/"

我知道在stackoverflow上有很多类似的问题,但是没有一个适合我,因为它们在字符串中只有一次数字,而在我的情况下,它在整个字符串中出现多次。

I tried this

   newUrl = existingUrl.replace(/abc'/[0-9]+/g, function(match, number) {
    return parseInt(number)+1;
    });

试试这个:

var result = url.replace(/('d+)'/$/, function(x){ return parseInt(x, 10) + 1 + '/' })

或与ecmascript-6:

var result = url.replace(/('d+)'/$/, x => parseInt(x) + 1 + '/' )