Stop writing empty string after all matches replaced if no more content left