I have a long string where I need to increment every number within it, leaving the rest of the text as it is.
I’m using this function
newHtml = newHtml.replace(/\d+/, function (val) { return parseInt(val) + 1; });
Which works great on numbers that are in free text but fails when the numbers are surrounded by square brackets. Example:
<input id="Form[0]_Phone" name="Form[0].Phone" type="text" value="">
Needs to become
<input id="Form[1]_Phone" name="Form[1].Phone" type="text" value="">
I’ve used this example to try and help, and I’ve tried a few variations but my regex skills have failed me.
Any assistance much appreciated.
There is nothing in your pattern causing the described behaviour – numbers in square brackets should also be affected. One obvious issue is you’re affecting only the first number found, not all – add the
gglobal flag after the closing forward slash of the pattern.Works for me – see this Fiddle: http://jsfiddle.net/ypUmg/