How to count the number of lines from a string in JavaScript

1 Answer

0 votes
const s = "line1\nline2\nline3\nline4\nlin5\nline6";

console.log(s.split(/\r\n|\r|\n/).length);

  
    
    
    
/*
run:
    
6
    
*/

 



answered Aug 19, 2021 by avibootz

Related questions

1 answer 221 views
1 answer 272 views
2 answers 299 views
1 answer 207 views
1 answer 233 views
1 answer 234 views
1 answer 336 views
...