Given a string, I would like to replace all ab
s by a
, and all single a
s by 1
, such that
X_ab_X-a%X
becomes
X_a_X-1%X
Replacing all a
s or all ab
s alone is pretty easy (here in JS)
mystring.replace(/a/gm, '1');
// mystring.replace(/ab/gm, 'a');
but I'm not seeing how to combine the two. All possible substrings can occur in mystring
such that first replacing ab
by TMP_UNIQUE_STRING
won't work.
(Perhaps regexes aren't the right tool for the job.)