better regex replacement handling