The answer is, as often, it depends.
In general, \n
and \r
are not the same. Traditionally, in regex engines
\n
maps on most platforms (including Unix, DOS/Windows) to the ASCII LF
character. On (classic) Mac OS systems (and old OS X versions), maps to the ASCII CR
character.
\r
in turn, maps to the ASCII CR
character, but on (old) Mac OS systems to LF
.
As time goes on, the old Mac-style tends to become irrelevant.
To prove this at least in part, here is a browser shot running Safari 9.1 on Mac OS 10.8 that matches \r
(result) and \r?\n
, (result) against a single line break - only when \n
is present in the regex there is a match.
However, there are still exceptions in JavaScript. For instance, if you define a multiline string using a template literal you always get a line feed - regardless of the OS-specific new-line definition. Explanation.
Nonetheless, if you define a string literal like '\r\n'
in your source code, or read text from a file stream that contains OS-specific new-lines, etc. you have to deal with it.
To answer your initial quest,
\r?\n
usually is a safe bet to remove excess new lines.
Or, if really have to deal with the old Mac-style use \r\n?|\n