Since VBScript doesn’t support lookbehinds, I’m looking for an alternative solution.
I have the string ‘\E\F\’.
I want to replace \F\ with ‘~’, but ONLY if it is not preceded by a \E.
After the substitution, I want ‘\E\F\’ to be ‘\E\F\’.
If the string was ‘randomText\F\’, I would want it to look like ‘randomText~’ after the substitution.
Solution:
I just decided to StrReverse it and do a negative forward lookahead. It’s not the most elegant solution, but it seems to work in this case.
Dim regEx, str1
str1 = StrReverse("The quick \F\ brown \E\F\ dog.")
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Pattern = "\\F\\(?!E\\)"
regEx.Global = True
ReplaceTest = regEx.Replace(str1, "%")
VBScript doesn’t support look-behind assertions. But try this:
Or this:
Replace the match with
$1~(first submatch and~).Here’s an explanation: In general there are two situations: If there is no or just one character before
\F\(^.?), everything is ok. But if there are at least two characters before\F\, we need to make sure, that these characters are not\E. So we say, that the two preceeding characters are either\followed by any arbitrary character ([^\\].), or\followed by any character other thenE(\\[^E]).That construct ensures that every other combincation except
\Eis allowed.The same applies to the second expression. But here we use the negative look-ahead assertion to ensure that the two characters before
\F\is not\E.