Fixed the replace method issue by using replaceFirst instead