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
Add a comment  | 

4 Answers 4

Reset to default 22

If 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