test and implementation for `replace/3`