我的 csv 数据看起来像这样
1,2,"hi,how ar you?",abc
3,5,a,b,c
expected output should be
4
5
我的 csv 数据看起来像这样
1,2,"hi,how ar you?",abc
3,5,a,b,c
expected output should be
4
5
'1,2,"hi,how ar you?",abc'.match(/"[^"]*"|[^,]+/g).length
4
'3,5,a,b,c'.match(/"[^"]*"|[^,]+/g).length
5
试试这个源自另一个帖子的方法:
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;
}