Sometimes, we want to split string in JavaScript by line breaks.
In this article, we’ll look at how to split string in JavaScript by line breaks.
How to split string in JavaScript by line breaks?
To split string in JavaScript by line breaks, we call the string split method with a regex.
For instance, we write
const array = str.split(/\r?\n/);
to call str.split with a regex that matches \r\n or \n to split str into an array with either as separators.
Conclusion
To split string in JavaScript by line breaks, we call the string split method with a regex.
