'text file upload line by line getting "\r"
hello everyone I want to get the data line by line I have done it successfully now I want line by line plus word by word so I used this code
var openFile = function(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
var node = document.getElementById('output');
node.innerText = text;
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++){
console.log(lines[line].split(" "));
}
};
reader.readAsText(input.files[0]);
};
the result is shocking in the console I am getting \r with the string

Solution 1:[1]
I think you should not split('\r\n') as different OS'es use different character to end line.
From what I read in this post, Windows uses '\r\n', MacOS uses '\r' and Unix uses '\n'.
Since this is JavaScript, I assume you need an answer to cover most, if not all, possibilities. So, for a complete answer, I believe you should do the following.
let lines='';
if (navigator.userAgent.indexOf('Win')!=-1)
lines = this.result.split('\r\n');
else if (navigator.userAgent.indexOf('Mac')!=-1)
lines = this.result.split('\r');
else
lines = this.result.split('\n');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stratis Dermanoutsos |
