Monday, 3 April 2017

nodjs & JS 'split' methods return different results

split a text from a txt file(utf16le ,environment is macos);

i use fs read the aaa.txt ,and split('\n\n'),the result length is 1. i render the data on page, and use jquery read the content ,use split('\n\n'), the result length is 6(right result)

the aaa.txt

1

a



2

b



3

c

d



4

e



5

f



6

g


node code:

 fs.readFile(__dirname+'/aaa.txt','utf16le',function (err,data) {
if(err){console.log(err);}
else {
  // console.log(data);
  // console.log(data);
  var textSubtitles = data.split('\n\n');
  console.log('length');
  console.log(textSubtitles.length);// the result is 1!!!
  res.render('aaa.ejs', {
        content:data
    })
}

jquery code:

 let sss = $('#fff').text().split('\n\n'); 

console.log(sss.length);// the result is 6!!!

sss is the data render by nodejs, the same text.



via panda happy

No comments:

Post a Comment