JavaScript csv 去除所有逗号的行



All,

我认为在 csv 中去除所有逗号行的最快方法是使用 reg 表达式,对吗?

如果我有这样的文件

jon,lastname,1/1/1970
bob,lastname,1/1/1970
sally,lastname,1/1/1970
,,,
,,,
anne,smith,1/1/1970
,,,

JavaScript中只用逗号去掉行的最快方法是什么?

我在javascript中尝试了很多不同的方法,但没有运气。 我的想法是从行的开头测试任何东西,并寻找逗号或换行符的多个实例。 当我尝试时,一切都会恢复真实

for (let i = 0; i < rows.length; i++) {
console.log('test', /^([,n]*)/.test(rows[i]));
}

感谢 琼普尔

这将有所帮助,

const input = `
jon,lastname,1/1/1970
bob,lastname,1/1/1970
sally,lastname,1/1/1970
,,,
,,,
anne,smith,1/1/1970
,,,
`;
const lines = input.trim().split(/n/);
const cleaned_lines = lines.filter(i => !/^[,]+$/.test(i)).join('n');
console.log(cleaned_lines);

最新更新