Baner Baner - 11 months ago 32
Node.js Question

Creating a new line after every 8th comma using string.prototype

rl.on('line', (line) => {
if (line.includes('OBJECT', 0) && !line.includes('__',0) && !line.startsWith('_', 51)) {
line= line.trimRight();
line= line.trimLeft();
line = line.replace(/\s+/g, ' ');
line = line.replace(/ /g, ',');

my code above parses a file using read line. Now I am having difficulty adding a new line after every 8th comma. Any ideas? Right now the parsed text file is showing up like in one big line


so I want to create a new line right before 112 so the file is parsed nicely.

end goal-



I'll probably use string replace with the following regular expression:

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') );