How to Split a String by Newline in JavaScript

avatar
Borislav Hadzhiev

Last updated: Jan 9, 2023
4 min

banner

# Split a String by Newline in JavaScript

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.

index.js
const str = 'bobby\nhadz\r\ncom'; const result = str.split(/\r?\n/); // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);

split string by newline

We passed a regular expression to the String.split() method.

The forward slashes / / mark the beginning and end of the regular expression.

We want to replace both \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.

# Using an alternative regular expression

You can also use a character class [] to split a string by newline.

index.js
const str = 'bobby\nhadz\r\ncom'; const result = str.split(/[\r\n]+/); // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);

using alternative regular expression

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.

# Remove empty strings after splitting

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.

index.js
const str = 'bobby\nhadz\r\ncom\n'; const result = str.split(/\r?\n/); // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com', '' ] console.log(result);

remove empty strings after splitting

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.

index.js
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.

index.js
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.

index.js
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.

# Using the os.EOL property in Node.js

If your code runs in Node.js, you can only use the os.EOL property.

index.js
import os from 'os'; const str = 'bobby\nhadz\r\ncom'; const arr = str.split(os.EOL); console.log(arr); // ๐Ÿ‘‰๏ธ [ 'bobby', 'hadz\r', 'com' ]

using the os eol property in node js

However, the os.EOL property only splits on the operating system-specific end-of-line marker:

  • \n on POSIX
  • \r\n on Windows

I'm on Linux, so the os.EOL property for me is \n.

index.js
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.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev