Can you increment a numeric substring using regex/replace? For example, if the end of a string (like window location) contains #img-{digit}
, is it possible to use regex to replace the digit with +1
?
I know how to match the hash, but extracting the number, which could be more than 2 digits like 12
, is my challenge.
var loc = window.location,
locstr = loc.match(/#img-\d+/),
// untested:
locrep = locstr.replace(/\d/, Number($1) + 1);
If the current hash is #img-4
, I need a JavaScript snippet that changes it to #img-5
.