panda happy
panda happy

Reputation: 21

nodejs & 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.

Upvotes: 1

Views: 61

Answers (1)

panda happy
panda happy

Reputation: 21

it is because the string from text is

[ '1\r\na\r\n\r\n2\r\nb\r\n\r\n3\r\nc\r\nd\r\n\r\n4\r\ne\r\n\r\n5\r\nf\r\n\r\n6\r\ng\r\n' ]

i need to use split('\r\n\r\n') instead split('\n\n')

Upvotes: 1

Related Questions