按\ n字符拆分似乎不起作用 [英] Splitting by \n character doesn't seem to work
问题描述
我从服务器获取结果,其中项目用新行分隔,所以我做了:
I'm getting a result from a server where items are separated with new lines, so I do a:
split('\n');
split('\\n');
它不起作用!在Chrome上进行调试时, \
消失了(我看到拆分('n'))。
It doesn't work! the \
disappears while debugging on Chrome (I see split('n')).
如何制作它有效吗?
示例数据(从chrome调试器复制):
Sample data (copy from debugger of chrome):
יופיוקוסמטיקה|10↵בידור ותרבות|9↵לילדולתינוק| 3↵תיירות| 4↵תכשיטים| 5
"יופי וקוסמטיקה|10↵בידור ותרבות|9↵לילד ולתינוק|3↵תיירות|4↵תכשיטים|5"
推荐答案
如果结果有文字字符\ n
在其中,您需要转义 \
。
If the result has the literal characters "\n"
in it, you need to escape your \
.
split('\\n');
另一种可能性是你有 \\\\ n
字符串中的序列。如果是这样,请执行以下操作:
Another possibility is that you have \r\n
sequences in the string. If so, do this:
split('\r\n');
...虽然 .split('\ n')
应该仍然有用。
...although the .split('\n')
should still work.
或者如果它只用 \ r
序列发送它们,你会这样做:
Or if it is sending them with just \r
sequences, you'd do:
split('\r');
如果您不确定,请执行以下操作:
If you're not sure, do this:
split(/\r\n|\n|\r/);
这篇关于按\ n字符拆分似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!