check whether string contains a line break
Line breaks in HTML aren't represented by \n
or \r
. They can be represented in lots of ways, including the <br>
element, or any block element following another (<p></p><p></p>
, for instance).
If you're using a textarea
, you may find \n
or \r
(or \r\n
) for line breaks, so:
var text = $("#theTextArea").val();
var match = /\r|\n/.exec(text);
if (match) {
// Found one, look at `match` for details, in particular `match.index`
}
Live Example | Source
...but that's just textarea
s, not HTML elements in general.
var text = $('#total-number').text();
var eachLine = text.split('\n');
alert('Lines found: ' + eachLine.length);
for(var i = 0, l = eachLine.length; i < l; i++) {
alert('Line ' + (i+1) + ': ' + eachLine[i]);
}