var my_data = document.getElementById("txt_area_in_question").value;
alert("The total paragraphs in the text area are: "+
my_data.split("\n\n").length);
Теперь, это не учитывает многократные переводы строк без текста ... так:
Some text
Some more text
Some more text
вернет 5
вместо 3
Решение состоит в том, чтобы убрать все пробелы и вернуть то, что осталось:
var my_data = document.getElementById("txt_area_in_question").value;
my_data = my_data.split("\n\n");
var g = my_data.length;
var i = 0;
var strip_whitespace = /\s+/gi;
while (g >=0) {
g--;
var tmp = my_data[g];
tmp = tmp ? tmp .replace(strip_whitespace,"") : tmp;
if( tmp && tmp.length > 1 ) {
i++;
}
}
alert("The total paragraphs in the text area are: "+i); //Will properly alert 3
См .: http://jsfiddle.net/UBWpJ/