This doesn't collapse "a \nb" into "a\nb". It's difficult to tell from the desired output (because I can't see spaces at the ends of lines …), but it seems that the poster may have wanted that to happen.
I thought I caught that case but, as you suggest, the white space on the end of the line is hard to see! The following simplifies the regex and fixes that case at the cost of complicating the substitution: