Better handling of preceding whitespace in the replace method