S&R: Rework the replacing code and add \L & \U support
Unify the replace code to one place and use moving-ranges to keep track of the positions of the matches after a replace.