rl.on('line', (line) => {
line= line.trimRight();
line= line.trimLeft();
line = line.replace(/\s+/g, ' ');
line = line.replace(/ /g, ',');
lines.push(line);
}
});
我上面的代码使用read行解析文件。现在我很难在每个第8个逗号之后添加一个新行。有任何想法吗?现在,解析后的文本文件就像一个大行一样显示
答案 0 :(得分:2)
我可能会将字符串replace
与以下正则表达式一起使用:
var result = line.replace(/(?:[^,]+,){8}/g, '$&\n');
var line = '110:,20000004,2,OBJECT,LOCAL,DEFAULT,8,DAC0_Cmd_Local,112:,20000006,2,OBJECT,LOCAL,DEFAULT,8,DAC1_Cmd_Local,';
console.log( line.replace(/(?:[^,]+,){8}/g, '$&\n') );
答案 1 :(得分:1)
ggplot(df1,aes(x = fakeY,y = fakeZ,fill = fakeX)) +
stat_boxplot(geom = "errorbar",position = "dodge",lwd = 1.75) +
geom_boxplot(position = "dodge",lwd = 1.75,fatten = 1.25,show.legend = FALSE) +
scale_fill_manual(values = c("dim gray", "ivory3")) +
scale_x_discrete(labels = c('2013','2014','2015'))
答案 2 :(得分:0)
解决方案在我的方式有点复杂,但VisioN答案真是令人惊讶,而且我最喜欢的是代码行数较少:
var str = '110:,20000004,2,OBJECT,LOCAL,DEFAULT,8,DAC0_Cmd_Local,112:,20000006,2,OBJECT,LOCAL,DEFAULT,8,DAC1_Cmd_Local,';
var itemPerLine = 8;
var result =
str.split(',')
.reduce(function(r, v, i) {
if (!(i % itemPerLine))
r.push([]);
r[r.length - 1].push(v);
return r;
}, [])
.map(v => v.join(','))
.join('\n');
console.log(result);