Newline \n problem in JS

15,224

Solution 1

I found my own solution to my problem.
After using random special characters, \r character used for carriage return worked like a charm for my problem.
It acted like a newline \n character or at least it did its job in my case.
Thanks everyone for answers and helpful comments.

Solution 2

Try using this line

/\n/

Instead of this one

"\n"
Share:
15,224
Oralet
Author by

Oralet

Updated on June 04, 2022

Comments

  • Oralet
    Oralet almost 2 years

    I am reading a file with xmlHttp object and splitting the responseText with newlines by split method.

    But "\n" character literally doesn't work. It acts like an error in my code and causes my code not even function. Here is the line:

    var lines=myPlaylist.responseText.split("\n");
    

    There is no error if I split the array myPlaylist with other characters. Just \n causes problem which I fail to understand.

    At first, I thought the error was due to white-space:nowrap since I execute my code on Chrome. Though I never used white-space in anywhere, I tried to set it to normal but it didn't work. Similarly, I tried my code on other browsers (Firefox, IE etc), it didn't work either. Looks like I have a problem with using \n. Is there any other way to use newline or error with my code?

    And by the way, error seems to be a syntax error since it does not just ignore \n character. Simply causes my code not to work

    EDIT: An example responseText

    [playlist]
    
    File1=http://localhost:7000/Videos/Big%20Buck%20Bunny%20Trailer.ogv
    Title1=Bunny Trailer
    Length1=23
    
    File2=http://localhost:7000/Videos/Dizzy%20Cat%20Video.ogv
    Title2=Kedi 
    Length2=33
    
    NumberOfEntries=2
    
    Version=2
    
  • Olical
    Olical over 13 years
    Looks like Chuck has the answer though. /\r\n|\r|\n/
  • Oralet
    Oralet over 13 years
    myPlaylist is defined like this : var myPlaylist = new XMLHttpRequest(); So it must have that property. And i couldn't execute mycode with your addition. Seems like problem still continues
  • Olical
    Olical over 13 years
    Oh! None of us suggested to use the global flag! I think that may be what wrong. @cantbereached Try some of the things you have tried before with a g after the RegExp.
  • Olical
    Olical over 13 years
    @cantbereached Regarding the AJAX, you may want to allow for cross browser like this because your method won't work in IE: github.com/Wolfy87/Spark/blob/master/src/ajax.js#L3
  • Martijn
    Martijn over 13 years
    @cantbereached: what browser are you using? Does the Javascript console have anything useful to say?
  • Oralet
    Oralet over 13 years
    I use both chrome and firefox. My program is kind of a file browser and it functions with keyboard navigation. When I use \n or any of these solution, I can't even navigate through menus. That's why I think it is a syntax problem
  • Martijn
    Martijn over 13 years
    Three questions: 1) does the Javascript console in Firefox say anything? 2) What does: jsbin.com/amano4 do? 3) Are you using any server-side code in your page (e.g. PHP)?
  • Oralet
    Oralet over 13 years
    1) No 2) An alert box saying "Lines found: 3" and Hello World on the page 3) No
  • Martijn
    Martijn over 13 years
    Ok, so there’s something in your page that’s breaking JS or the JS parser, since that jsbin page uses the exact code from my answer above. Does it work if you extract the Javascript, save it to a separate file, and reference that using <script src="file.js"></script>?
  • Oralet
    Oralet over 13 years
    I tried your suggestion but no good. I can't use anything meaning newline anywhere in my code. It creates error. I tried to add \n to an innerText and it didn't work either :/
  • Martijn
    Martijn over 13 years
    Really weird. Your page isn't publicly available, by any chance?
  • Martijn
    Martijn over 13 years
    Another suggestion: since \n seems to break your code, does it work when you avoid the \n notation, e.g. with split(String.fromCharCode(10))?
  • Oralet
    Oralet over 13 years
    I found it mate. I just used \r for newline instead of using \n or \r\n or /\n/. Thanks for your sincere help
  • WestCoastProjects
    WestCoastProjects over 8 years
    i accidentally hit downvote. Just upvoted to "make up" for it ;)
  • J.Ko
    J.Ko over 5 years
    Voting up because this worked for me. In my case, the string I had to split was from JSON.stringify(str) so new line chars \n was actually literally \n So this helped