It looks like regex /\r\n|\r|\n/
handles CR, LF, and CRLF line endings, their mixed sequences, and keeps all the empty lines inbetween. Try that!
function splitLines(t) { return t.split(/\r\n|\r|\n/); }
// single newlines
console.log(splitLines("AAA\rBBB\nCCC\r\nDDD"));
// double newlines
console.log(splitLines("EEE\r\rFFF\n\nGGG\r\n\r\nHHH"));
// mixed sequences
console.log(splitLines("III\n\r\nJJJ\r\r\nKKK\r\n\nLLL\r\n\rMMM"));
_x000D_
You should get these arrays as a result:
[ "AAA", "BBB", "CCC", "DDD" ]
[ "EEE", "", "FFF", "", "GGG", "", "HHH" ]
[ "III", "", "JJJ", "", "KKK", "", "LLL", "", "MMM" ]
You can also teach that regex to recognize other legit Unicode line terminators by adding |\xHH
or |\uHHHH
parts, where H
's are hexadecimal digits of the additional terminator character codepoint (as seen in Wikipedia article as U+HHHH
).