I want to catch numbers appearing anywhere in a string, and replace them with "(.+)".
But I want to catch only those numbers which have an even number of %
s preceding them. No worries if any surrounding chars get caught up: we can use capture groups to filter out the numbers.
I'm unable to come up with an ECMAscript regular expression.
Here is the playground:
abcd %1 %%2 %%%3 %%%%4 efgh
abcd%12%%34%%%666%%%%11efgh
A successful catch will behave like this:
If you have realised, the third attempt is almost working. The only problems are in the second line of playground. Actually, what I wanted to say in that expression is:
Match a number if it is preceded by an even number of %
s AND either of the following is true:
%
.Is there a way to match the absence of a character?
That's what I was trying to do by using \0
in the third attempt.
You can use (?:[^%\d]|^|\b(?=%))(?:%%)*(\d+)
as a pattern, where your number is stored into the first capturing group. This also treats numbers preceded by zero %-characters.
This will match the even number of %-signs, if they are preceded by:
%%1%%2
)You can see it in action here