The string may look like this:
"note 2 2_ someothertext_2 note 2finally_2222 but how about mymomsays2."
I want turn turn it into this:
"note 3 3_ someothertext_3 note 3finally_2222 but how about mymomsays3."
Obviously this means
.replace('2','3') is out of the picture so I went to regex. I find it easy to get an exact match when I am dealing with string start to end ie:
/^2$/g. But that is not what I have. I tried grouping, digit only, wildcards, etc and I can't get this to match correctly.
Any help on how to exactly match a number (where 0 <= number <= 500 is possible, but no constraints needed in regex for range) would be greatly appreciated.
The task is to find (and replace) "single" digit 2, not embedded in a number composed of multiple digits.
In regex terms, this can be expressed as:
The regex for the first condition is straightforward - just
In other flavours of regex, e.g. PCRE, to forbid the previous
regex does not support it.
So, to circumvent this, we must:
The last condition, fortunately, can be expressed as negative lookahead,
So the whole regex is:
Having found such a match, you have to replace it with the content of the first capturing group and 3 (the replacement digit).
You can use negative look-ahead:
If look behind is supported: