Given the following string
XXXX Units[4].Test XXXX
I would like to replace each occurrence of Unit[x] with Unit[x+1].
I am trying to achieve this using a regular expression, as the name Units needs to be literally matched first.
I am trying something like this:
test.replace(/(?:Units\[|\_+)(\d+)/g, function(m, i) { return parseInt(m) + 1 })
My problem is that without positive lookbehind I cannot seem to match only the digits. I am trying to build the regex in such a way that only the digits form part of the capture group, and I can therefore replace them.
It's easy to do in c#, but the javascript match function works a little differently.
Despite the non capture specification of (?: the Units[ seems to form part of the match. Since javascript does not support look behind, I am at a loss as to how to complete this.
(?:just means it's not in a capturing group, it's still in the overall substring that's parsed, and then your digits are in the first captured group.String.prototype.replace()this may be of help to you.