DEV Community

Discussion on: Let's talk about how powerful split function is

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

Well, if all you want is to count lines with assumption that \n means new line, i would suggest using something simpler (i assume it would be faster, but thats for someone else to test out).

const text = "hello \n world \n !!!"

const count = (text.match(/\n/g) || []).length + 1;

count // 3
Collapse
 
jalal246 profile image
Jalal 🚀

Hi Paweł, thanks for the reply. I am not sure if using match instead of split is simpler or just another way to count lines. And there's another point, using split, enables us to count words, characters, and spaces. How do you achieve this goal with match? I think this adds more complexity instead of developing what we already have.