Increment a number in a string in with regex

前端 未结 4 936
说谎
说谎 2020-11-28 13:15

I get the name of an input element, which is a string with a number (url1). I want to increment the number by 1 (url2

4条回答
  •  鱼传尺愫
    2020-11-28 13:42

    You can use replace and pass it a function to use to replace the matched section:

    str.replace(/\d+/, function(number) { return parseInt(number, 10) + 1; });
    

提交回复
热议问题