简体   繁体   中英

javascript new line split is splitting at every n char

i am using below code to split the data but unfortunately not able to achieve it.

i have tried below things

var lines = data. split(/\r\n|\r|\n/g);

data string is "this is ended here
                another line "

what is happening is it is returing like below

this is e
ded herea
other li
e

actually it is splitting at every character "n" i am going crazy.. but not able to find out what is issue.

could anybody please share some light on it.

UPDATE -

i think something is wrong when i put it on godaddy hosting.. on my desktop it is working fine.. any idea?? This is server site javascript.

thank you

UPDATE - 20/08/13 Despite various try my server level javascript split did not work, so i moved server level javascript to client level... and everything is working fine...

Thank you everyone for answering this issue. here i can accept only one answer as correct.. though all the answers provided below are correct.

Your regex must be wrong and you have written

split(/\n\r|\r|n/);

or something instead of

split(/\n\r|\r|\n/);

This code works for me in Chrome and FF :

var data = "this is ended here \n another line ";
var lines = data.split(/\\r\\n|\\r|\\n/g);
console.log(lines);

It writes :

["this is ended here ", " another line "]

See fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM