Alex Rivera | Logout

What is a cross platform regex for removal of line breaks?

Asked 2010-07-10T11:36:12.040
32

I am sure this has been asked before, but I cannot find it.

Basically, assuming you are parsing a text file of unknown origin and want to replace line breaks with some other delimiter, is this the best regex, or is there another?

(\r\n)|(\n)|(\r)

Edit
Report

2 Answers

45

Fletcher - this did get asked once before.

Here you go: Regular Expression to match cross platform newline characters

  • Spoiler Alert!

The regex I use when I want to be precise is "\r\n?|\n".

answered 2010-07-10T11:40:08.130
-1

Just replace /[\r\n]+/g with an empty string "".

It'll replace all \r and \n no matter what order they appear in the string.

answered 2010-07-10T11:39:18.313

Your Answer