1

我的 csv 数据看起来像这样

1,2,"hi,how ar you?",abc
3,5,a,b,c
expected output should be
4
5
4

2 回答 2

1
'1,2,"hi,how ar you?",abc'.match(/"[^"]*"|[^,]+/g).length
4
'3,5,a,b,c'.match(/"[^"]*"|[^,]+/g).length
5
于 2013-08-02T05:05:27.847 回答
0

试试这个源自另一个帖子的方法:

   var csvLines = '1,2,"how ar you?",abc' + "\n" + '1,4,"fine",6,7';
   var splitByChars = ',';

   var totalCount = 0;
   var linesArray = csvLines.split("\n");

   var lineCount = 0;

   while (lineCount < linesArray.length) {
       totalCount += StringCount(csvLines, splitByChars);
       lineCount++;
   }

   alert(totalCount);

   function StringCount(stringToSplit, splitBy) {
       var words = stringToSplit.split(splitBy);
       return words.length;
   }
于 2013-08-02T05:05:49.353 回答