Count number of lines in CSV with Javascript

后端 未结 4 562
离开以前
离开以前 2021-01-06 11:47

I\'m trying to think of a way to count the number of lines in a .csv file using Javascript, any useful tips or resources someone can direct me to?

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 12:25

    You can use the '.' to match everything on a line except the newline at the end- it won't count quoted new lines. Use the 'm' for multiline flag, as well as 'g' for global.

    function getLines(s){
        return s.match(/^(.*)$/mg);
    }
    
    alert(getLines(string).length)
    

    If you don't mind skipping empty lines it is simpler- but sometimes you need to keep them for spaceing.

    function getLines(s){ return s.match(/(.+)/g); }

提交回复
热议问题