在使用 Nodejs 编写代码时,我不小心遇到了这种奇怪的错误现象。
我从一个表单中获取输入,该表单由一系列 ID 组成,用换行符分隔(按 Enter)
为了处理它们,我使用split函数将它们拆分为一个数组:
var array = orig_input.split('\n');
出于调试目的,我打印它们。
console.log('orig_input = ' + input);
console.log('array = ' + array);
好吧,这是第一个奇怪的输出:
orig_input = 1111
2222
,2222
看!假定的输出字符串'array = 1111'被吞掉了!
我的第一个想法是array[0]有一些删除字符。
所以我改变了代码来做一些测试,比如:
console.log('orig_input = ' + input);
console.log('***********************************' + array[0] + array[1]);
哇,又一个奇怪的输出:
orig_input = 1111
2222
2222*******************************1111
似乎有一些奇怪的包装或什么的。
最后,经过一些无聊的测试和调试,结果是
I used the wrong split character, it should be '\r\n', instead of '\n'
我们知道前者是MS风格的换行,后者是*nix风格的换行。
所以问题是
Is this a bug of console.log function in **Nodejs** ?
Anyone would explain this?