Last updated: Mar 4, 2024
Reading timeยท4 min
Use the String.split()
method to split a string by newline, e.g.
str.split(/\r?\n/)
.
The split
method will split the string on each occurrence of a newline
character and will return an array containing the results.
const str = 'bobby\nhadz\r\ncom'; const result = str.split(/\r?\n/); // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);
We passed a regular expression to the String.split() method.
The forward slashes / /
mark the beginning and end of the regular expression.
\r
and \n
characters because the line breaks vary depending on the operating system.For example, Windows uses \r\n
as the end of line character, whereas \n
is
the default in Unix.
The question mark ?
matches the preceding item (\r) 0 or 1 times.
In other words, the \r
might or might not be there.
You can also use a character class []
to split a string by newline.
const str = 'bobby\nhadz\r\ncom'; const result = str.split(/[\r\n]+/); // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);
The square brackets []
are called a character class and match any of the
characters between the brackets (\r
and \n
in our case).
The plus +
matches the preceding item (the character class) 1 or more
times.
There might be an edge case you need to handle.
If the string ends with a newline character, the last array element would be an empty string.
const str = 'bobby\nhadz\r\ncom\n'; const result = str.split(/\r?\n/); // ๐๏ธ [ 'bobby', 'hadz', 'com', '' ] console.log(result);
We split the string on newline characters, however, there's nothing after the last newline, so we get an empty string as the last array element.
We can handle this using the Array.filter()
method.
const str = 'bobby\nhadz\r\ncom\n'; const result = str.split(/\r?\n/).filter(element => element); // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);
The function we passed to the Array.filter() method gets called with each element in the array.
On each iteration, we return the element as is.
The filter()
method returns a new array that only contains the elements that
meet the condition.
Empty strings are falsy values, so they don't get added to the new array.
An alternative and more concise approach would be to pass the Boolean()
constructor to the filter()
method.
const str = 'bobby\nhadz\r\ncom\n'; const result = str.split(/\r?\n/).filter(Boolean); // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);
The Boolean()
constructor takes each element from the array, converts it to a
boolean and returns the result.
console.log(Boolean('test')); // ๐๏ธ true console.log(Boolean('')); // ๐๏ธ false
Empty strings are falsy values and get converted to false
, therefore they
don't get added to the new array.
If you need to split a string by spaces, check out the following article.
os.EOL
property in Node.jsIf your code runs in Node.js, you can only use the os.EOL
property.
import os from 'os'; const str = 'bobby\nhadz\r\ncom'; const arr = str.split(os.EOL); console.log(arr); // ๐๏ธ [ 'bobby', 'hadz\r', 'com' ]
However, the os.EOL property only splits on the operating system-specific end-of-line marker:
\n
on POSIX\r\n
on WindowsI'm on Linux, so the os.EOL
property for me is \n
.
import os from 'os'; console.dir(os.EOL); // ๐๏ธ '\n'
This is why the string didn't get split on the \r\n
character in my case.
This approach should only be used when you are sure that the file is native to your operating system.
Even then, it is probably a better idea to use a universal solution based on a regular expression that handles both types of end-of-line markers.
const str = 'bobby\nhadz\r\ncom'; const result = str.split(/\r?\n/); // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);
Which approach you pick is a matter of personal preference. I'd go with
explicitly returning the array element as I find the Boolean
approach to be
too implicit and indirect.
You can learn more about the related topics by checking out the following tutorials: