How to count line row count in javascript

I want to count the number of rows in a row

I tried using this stackoverflow answer:

lines = str.split("\r\n|\r|\n"); return lines.length; 

in this line (which was originally a buffer):

  GET / HTTP/1.1 Host: localhost:8888 Connection: keep-alive Cache-Control: max-age=0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML,like Gecko) Chrome/15.0.874.121 Safari/535.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 

and for some reason I got the lines = '1'.

any idea how to make it work?

+61
javascript string split
Dec 13 '11 at 11:49
source share
8 answers

Using regex, you can count the number of lines as

  str.split(/\r\n|\r|\n/).length 

Alternatively, you can try the separation method as shown below.

 var lines = $("#ptest").val().split("\n"); alert(lines.length); 

working solution: http://jsfiddle.net/C8CaX/

+107
Dec 13 '11 at 11:54
source share

Another short, potentially more effective solution than split is:

 const lines = (str.match(/\n/g) || '').length + 1 
+22
May 6 '17 at 12:46
source share

For separation using regular expression /.../

 lines = str.split(/\r\n|\r|\n/); 
+9
Dec 13 '11 at 11:56
source share

Hm, yes ... what you do is completely wrong. When you say str.split("\r\n|\r|\n") , it will try to find the exact string "\r\n|\r|\n" . This is where you are mistaken. There is no such appearance in the whole line. What you really want is what David Hedlund suggested:

 lines = str.split(/\r\n|\r|\n/); return lines.length; 

The reason is because the split method does not convert strings to regular expressions in JavaScript. If you want to use regex, use regex.

+8
Dec 13 '11 at 12:01
source share

There are three options:

Using jQuery (download from jQuery ) - jquery.com

 var lines = $("#ptest").val().split("\n"); return lines.length; 

Using Regex

 var lines = str.split(/\r\n|\r|\n/); return lines.length; 

Or, recreating a for each cycle

 var length = 0; for(var i = 0; i < str.length; ++i){ if(str[i] == '\n') { length++; } } return length; 
+3
Dec 12 '13 at 18:15
source share

I did a performance test by comparing split with a regex, with a string and doing a for loop.

The for loop seems to be the fastest.

NOTE: this code as-is is useless for windows and macros, but should be ok for comparing performance.

Divide by line:

 split('\n').length; 

Split using regex:

 split(/\n/).length; 

Split using for:

 var length = 0; for(var i = 0; i < sixteen.length; ++i) if(sixteen[i] == s) length++; 

http://jsperf.com/counting-newlines/2

+3
Apr 13 '15 at
source share

Here is a working fiddle example

Just remove the extra \ r \ n and "|" from your reg.

+1
Dec 13 '11 at 11:56
source share

A better solution, because the function str.split ("\ n") creates a new array of strings separated by "\ n", which is heavier than str.match (/ \ n \ g). str.match (/ \ n \ g) creates an array of only matching elements. Which in our case is "\ n".

 var totalLines = (str.match(/\n/g) || '').length + 1; 
0
May 13 '19 at 6:57
source share



All Articles