Fix a bug in replace method if the rep is not empty or has a different
type than the given one