Check if line contains line break

So, I need to get the HTML text and check if it contains a line break. As I can see if it contains \n , because returning a string with val() does not contain \n , and I cannot detect it. I tried using .split("\n") , but it gave the same result. How can I do that?

One minute, IDK, why, when I add \n to textarea as a value, it splits and moves on to the next line.

+9
javascript jquery
Feb 28 '13 at 8:45
source share
2 answers

Line breaks in HTML are not represented \n or \r . They can be represented in a variety of ways, including the <br> element or any block element following the other ( <p></p><p></p> , for example).

If you use textarea , you can 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 it's just textarea s, not HTML elements at all.

+24
Feb 28 '13 at 8:47
source share
 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]); } 
+5
Feb 28 '13 at 8:49
source share



All Articles