Increment a number in a string based on the substring before it in JS

前端 未结 3 1335
独厮守ぢ
独厮守ぢ 2021-01-26 20:31

I need to increment a number in a url, the number is always preceded by page/ so like page/2/ however the url could be constructed any which way and ma

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 20:51

    You could look for a following slash and the end of the string and replace the number.

    let link = '//localhost:3000/insight/page/2/';
    let next = link.replace(/\d+(?=\/$)/g, n => +n + 1);
    
    console.log(next);

提交回复
热议问题