Languages
[Edit]
EN

JavaScript - how to split string by newline?

13 points
Created by:
AnnLen
1885

In this article, we're going to have a look at the problem how to split string to separated lines.

Quick overview:

  • It works on Linux and Windows:
// ONLINE-RUNNER:browser;

var text = '1\r\n2\r\n3';
var lines = text.split(/\r?\n/); //  \r\n  or  \n

console.log(lines);
  • It works on all operating systems:
// ONLINE-RUNNER:browser;

var text = '1\r\n2\r\n3';
var lines = text.split(/\r\n|\n\r|\n|\r/); //  \r\n  ,  \n\r  ,  \n  or  \r

console.log(lines);

 

Look on below problem description and examples to see how it works in practice.

1. Problem overview

Different operating systems have different new line symbols.

There are few most commonly used new line separations:

\nMultics, Unix and Unix-like systems (Linux, macOS, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others.
\r\nAtari TOS, Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM operating systems.
\rCommodore 8-bit machines (C64, C128), Acorn BBC, ZX Spectrum, TRS-80, Apple II series, Oberon, the classic Mac OS, MIT Lisp Machine and OS-9
\n\rAcorn BBC and RISC OS spooled text output.

Source: https://en.wikipedia.org/wiki/Newline

2. Universal new lines splitting example

This approach works with all operating systems. Presented example shows split to separated lines operation on mixed text.

// ONLINE-RUNNER:browser;

var LINE_EXPRESSION = /\r\n|\n\r|\n|\r/g; // expression symbols order is very important

var text = 'line 1\n' +
    'line 2\r' +
    'line 3\r\n' +
    'line 4\n\r' +
    'line 5';

var lines = text.split(LINE_EXPRESSION);

console.log(lines);

Note: expression symbols order is very important to prevent taking apart for new line symbols like \r\n or \n\r.

let's suppose we have HTTP protocol response:

HTTP/1.1 200 OK\r\nContent-Length: 25\r\nContent-Type: text/html\r\n\r\nHello world!\nSome text...

for \n or \r at begining of LINE_EXPRESSION variable we could get different number of new lines after splitting.

3. Microsoft Windows new lines splitting example

// ONLINE-RUNNER:browser;

var text = 'line 1\r\n' +
    'line 2\r\n' +
    'line 3\r\n' +
    'line 4\r\n' +
    'line 5';

var lines = text.split('\r\n');

console.log(lines);

Systems: Atari TOS, Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM operating systems.

4. Unix/Linux new lines splitting example

// ONLINE-RUNNER:browser;

var text = 'line 1\n' +
    'line 2\n' +
    'line 3\n' +
    'line 4\n' +
    'line 5';

var lines = text.split('\n');

console.log(lines);

Systems: Multics, Unix and Unix-like systems (Linux, macOS, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others.

5.  The classic Mac OS/OS-9 new lines splitting example

// ONLINE-RUNNER:browser;

var text = 'line 1\r' +
    'line 2\r' +
    'line 3\r' +
    'line 4\r' +
    'line 5';

var lines = text.split('\r');

console.log(lines);

Systems: Commodore 8-bit machines (C64, C128), Acorn BBC, ZX Spectrum, TRS-80, Apple II series, Oberon, the classic Mac OS, MIT Lisp Machine and OS-9. 

Native Advertising
50 000 ad impressions - 449$
ūüöÄ
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

‚̧ԳŹūüíĽ ūüôā

Join