admin管理员组文章数量:1198364
I'm getting a result from a server where items are separated with new lines, so I do a:
split('\n');
split('\\n');
It doesn't work! the \
disappears while debugging on Chrome (I see split('n')).
How to make it work?
Sample data (copy from debugger of chrome):
"יופי וקוסמטיקה|10↵בידור ותרבות|9↵לילד ולתינוק|3↵תיירות|4↵תכשיטים|5"
I'm getting a result from a server where items are separated with new lines, so I do a:
split('\n');
split('\\n');
It doesn't work! the \
disappears while debugging on Chrome (I see split('n')).
How to make it work?
Sample data (copy from debugger of chrome):
"יופי וקוסמטיקה|10↵בידור ותרבות|9↵לילד ולתינוק|3↵תיירות|4↵תכשיטים|5"
Share Improve this question edited Dec 4, 2011 at 19:56 Chen Kinnrot asked Dec 4, 2011 at 17:30 Chen KinnrotChen Kinnrot 21k17 gold badges81 silver badges142 bronze badges 2- And? Your question? The rest of the code? The string? – Ry- ♦ Commented Dec 4, 2011 at 17:32
- 1 provide a sample string that is being sent from the server. Also the code snippet that you have written to split the string – sv_in Commented Dec 4, 2011 at 17:35
4 Answers
Reset to default 22If the result has the literal characters "\n"
in it, you need to escape your \
.
split('\\n');
Another possibility is that you have \r\n
sequences in the string. If so, do this:
split('\r\n');
...although the .split('\n')
should still work.
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/);
I tried it with split("\n")
and it is working.
The choice of double or single quotes will make a difference in the way that Ruby treats the contents.
I worked on a page that runs under template engine, it removed the "\
" from the "\n
".
if you have '↵' character in your paragraph,
Firstly you must replace '↵' character to '\n' then
and if you have web project
you must use 'white-space: pre-line' in your css code.
I have some problem when I get paragraph from database paragraph coming with '↵' character and it not add new line in paragraph I used this solution for this problem.
本文标签: javascriptSplitting by n character doesn39t seem to workStack Overflow
版权声明:本文标题:javascript - Splitting by n character doesn't seem to work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738559216a2098901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论