The problem of detecting resettable backslashes in a regular expression has fascinated me several times, and only recently I realized that I was exaggerating it completely. There are several things that make it simpler, and as far as I can tell, no one here has noticed them yet:
Backslashes escape any character after them, and not just other backslashes. Thus, (\\.)* Will eat a whole string of escaped characters, regardless of whether they are a backslash or not. You do not need to worry about worms with even or odd numbers; just check the single \ at the beginning or end of the chain ( JavaScript solution> uses this).
Workarounds are not the only way to make sure that you start with the first backslash in the chain. You can simply search for a backslash character (or the beginning of a line).
The result is a short, simple template that does not require backreferences or callbacks, and is shorter than anything else I see so far.
/(?!<\\)(\\.)*\*/g
And the replacement string:
"$1%"
This works in .NET , which allows lookbehinds, and it should work for you in Perl. This can be done in JavaScript, but without lookbehinds or \G anchors, I see no way to do this in one layer. Ridgerunner's callback should work, like a loop:
var regx = /(^|[^\\])(\\.)*\*/g; while (input.match(regx)) { input = input.replace(regx, '$1$2%'); }
There are many names here that I learn from other questions related to regular expression, and I know that some of you are smarter than me. If I made a mistake, say so.
Justin morgan
source share