This is basically the same bug as #1401, but applied to replacements instead of --only-matching. Fixes #1739