Increment a number in a string in with regex

How about:

'url1'.replace(/\d+$/, function(n){ return ++n }); // "url2"
'url54'.replace(/\d+$/, function(n){ return ++n }); // "url55"

There we search for a number at the end of the string, cast it to Number, increment it by 1, and place it back in the string. I think that’s the same algo you worded in your question even.

Reference:

Leave a Comment