Fix #5770: Replace with $1 and so on works in Replace All mode, too

This fixes #5770, where regexp replace works incorrectly while in Replace All mode.

My question: Is there anything better than a do-while loop with an if to break (there must be anything better ;) )

@njx