I am just continuously hitting a wall with this one, I cannot solve it. I am trying to get a regex that executes as:
(axb|cxd)
except without repeating x in the expression (as it is really long encoding-matching expression). This is tested against a large string where I only need to match x for string replacement, so zero-width lookahead/behind is an option. Any ideas?
a,b,c, and d are reasonably small, so they can repeat if it makes the expression easier to form.
Thanks in advance.
If you really don’t want to repeat x in the regular expression, then you’ll have to apply some manual search logic. You can search for x with the regular expression and then, based on where it was found, look for a before it and b after it or c before it and d after it. But, that would likely be more work than just repeating it in the regular expression as in Gabe’s answer.
For example, you could do this:
But, honestly, it seems like it would just be easier to include x twice and let the regex engine do the dirty work for you:
Or, if it’s too cumbersome to put x in the string twice, then build the regex yourself using javascript string math: