Reputation:
In a csv file that has some blank lines in between and the last line is blank as well, how can I ignore them or let's say, jump to the next not empty line?
column1; column2; column3
value1; value2; value3
__________________this line is empty
value7; value8; value9
__________________this line (the last one) is empty
I obtain the file content:
var fileString = evt.target.result;
Than I do this to analize every single line and then to display it on my website:
var lines = fileString.split("\r\n");
Is there any possibility to write a loop that checks the blank lines and replace them? Like this:
var pattern = /\r?\n\r?\n/g;
for(var i = 0; i < fileString.length; i++){
if(lines === null){
lines.replace(pattern, '\n');
}
}
Upvotes: 0
Views: 3162
Reputation: 32511
If you have a blank line in between lines, that means you have two line terminators in a row. Most of the time that means you have \n\n
but on Windows, line terminators are preceded by a carriage return which means \r\n\r\n
. To get rid of empty lines, replace any double terminators by single terminators.
var input = 'a,b,c\r\n' +
'd,e,f\r\n' +
'\n' +
'g,h,i\n' +
'\n';
var pattern = /\r?\n\r?\n/g;
console.log(input.replace(pattern, '\n'));
This regular expression will match either \r\n
or just \n
(the \r?
means 0 or 1 matches of \r
) then using the global flag (g
) you can replace every match in the string with the string you pass as the second argument ('\n'
).
Warning: Attempting to destroy Terminators may result in a long-term battle for humanity.
Upvotes: 1