Search the textarea for "\n".
To be certain, do a regex search for pattern "\\n" and count the matches array, that will be the linebreak count.
cypher
2010-06-22 13:15:52
Search the textarea for "\n".
To be certain, do a regex search for pattern "\\n" and count the matches array, that will be the linebreak count.
Utilize the String split
function...
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]);
}
Using regular expressions, your problem can be solved like this (searching the textarea for the new line character "\n")):
//assuming $('#total-number') is your textarea element
var text = $('#total-number').val();
// look for any "\n" occurences
var matches = text.match(/\n/g);
// count them, if there are any
var breaks = matches ? matches.length : 0;
breaks
variable now contains number of line breaks in the text.