Languages
[Edit]
EN

JavaScript - split string by new line character

34 points
Created by:
Gigadude
791

In this article, we're going to have a look at the problem of how to splitĀ string to separate lines in JavaScript.

Note: by default in JavaScript \n newline character should be used, but modern applications exchange information with external endpoints that forces programmers to deal with different cases - this article provides quick solutuioins for the problem.

Quick solution:

// ONLINE-RUNNER:browser;

var string = 'line 1\n' +
             'line 2';

var lines = string.split(/\r?\n/);  // <--------- splits string

console.log(lines);

Note: the above solution was inspired with new line characters that are usedĀ on MacOS, Linux and Windows.

Ā 

Alternative quick solutionsĀ 

  • It works on all operating systems (even older one):
// ONLINE-RUNNER:browser;

var string = 'line 1\r\n' +                   // \r\n - used as new line symbol
             'line 2\n' +                     // \n   - used as new line symbol
             'line 3';

var lines = string.split(/\r\n|\n\r|\n|\r/);  // split by:     \r\n  \n\r  \n  or  \r

console.log(lines);                           // array items:  line 1, line 2, line 3
  • It is an alternativeĀ approach that uses \RĀ equivalent:
// ONLINE-RUNNER:browser;

var string = 'line 1\r\n' +         // \r\n - used as new line symbol
             'line 2\n' +           // \n   - used as new line symbol
             'line 3';

// https://dirask.com/posts/JavaScript-R-equivalent-in-RegEx-D9a2rD
//
var lines = string.split(/\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]/);

console.log(lines);                 // array items:  line 1, line 2, line 3

Ā 

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

1. Problem overview

Different operating systems have different newline symbols.

There are a 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. Problem solutions

2.1. Universal new lines splitting example

It is possible to propose some universal regular expression that lest to get a very good splitting result.

It is necessary to useĀ \r\n|\n\r|\n|\r as a pattern.

Go to this article to see details and runnable example.

2.2. Alternative new lines splitting example using \R equivalent

Some modern regular expression implementations provide \R predefined character class,
e.g.Ā Java, Notepad++, etc.

Most JavaScript engines don'tĀ support \R yet - so, it is required some trick.

Go to this article to see details and runnable example.

2.3. Microsoft Windows new lines splitting example

// ONLINE-RUNNER:browser;

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

var lines = string.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.

2.4. Unix/Linux new lines splitting example

// ONLINE-RUNNER:browser;

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

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

console.log(lines);

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

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

// ONLINE-RUNNER:browser;

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

var lines = string.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.Ā 

Ā 

See also

  1. JavaScript - universal new lines splitting symbol example

  2. JavaScript - \R equivalent in RegEx
  3. Node.js - split string by new line character (JavaScript)

Alternative titles

  1. js split lines
  2. JavaScript split on newline
  3. JavaScript split by newline
  4. JavaScript split line break
  5. split by new line in JavaScript
  6. JavaScript - make array from string by newline character
  7. JavaScript - split string into array by new line character
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - split string by new line character

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join