Split string in JavaScript and detect line break
Split string in JavaScript
var array = str.match(/[^\r\n]+/g);
OR
var array = str.split(/\r?\n/);
Using .split()
:
var str = `your text
that spans
multiple lines`
// Split the string on \n or \r characters
var separateLines = str.split(/\r?\n|\r|\n/g);
alert("Total number of separate lines is: " + separateLines.length);
Or using .match()
instead:
var str = `your text
that spans
multiple lines`
// Split the string on \n or \r characters
var separateLines = str.match(/[^\r\n]+/g);
alert("Total number of separate lines is: " + separateLines.length);
Hope that helps!