That's a much better solution than the original approach which is vulnerable to delimiters, backslash escape sequences, and ampersands in the replacement text. However, it suffers its own shortcoming: If the flag occurs in the replacement text, some (if not all) of the replacement text will be deleted.
Granted, that scenario is likely to be uncommon and it wasn't mentioned in the original problem statement, but since the fix is painless and also makes the solution more efficient, it seems like a good idea.