Updated the logic for replaceAll method.