4

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
Oralet
  • 349
  • 1
  • 2
  • 11
  • If my answer does fix your problem, remember to up vote and accept the answer. Just thought I would remind you due to you being new :) – Olical Feb 02 '11 at 12:40
  • 1
    If you find a solution to my problem, that will be the first thing I will do, don't worry ;) – Oralet Feb 02 '11 at 12:45
  • If the /\n/ isn't working, can you post a sample of your responseText that you're trying to split? –  Feb 02 '11 at 12:50

5 Answers5

3

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.

Oralet
  • 349
  • 1
  • 2
  • 11
1

Use \\n instead of \n i tried on my code and it is working fine

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
McGyver
  • 31
  • 1
  • 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 – J.Ko Sep 29 '18 at 21:45
1

Try using this line

/\n/

Instead of this one

"\n"
Olical
  • 39,703
  • 12
  • 54
  • 77
1

Here's an SO thread that will provide a bit more insight

JavaScript string newline character?

EDIT1: I just tested this and it splits appropriately on new lines. Can you post some of what you're trying to split?

<html>
    <script>
        function testSplit(value)
        {
            var lines = value.split(/\n/);
            alert(lines);
        }
    </script>
    <body>
        <textarea id="test" name="test" onblur="testSplit(this.value);">

        </textarea>
    </body>
</html>

EDIT2:

Can you try converting your responseText to an object and seeing what you get from it - sorry, just shooting from the hip here since I haven't had time to mock up anything for testing.

eval("var playlistResponse = ("+ myPlaylist.responseText +")");

Here's a somewhat old article that might be useful to you: http://www.peachpit.com/articles/article.aspx?p=443580&seqNum=4

Community
  • 1
  • 1
0

This should work without problem. Are you certain that myPlaylist has a responseText property, and that property is a string?

What happens if you catch an eventual error?

try {
  var lines = myPlaylist.responseText.split(/\n/g);
  alert(lines.length);
} catch (e) {
  alert(e.message);
}
Martijn
  • 13,225
  • 3
  • 48
  • 58
  • 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 – Oralet Feb 02 '11 at 12:51
  • 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 Feb 02 '11 at 12:51
  • 2
    @cantbereached Regarding the AJAX, you may want to allow for cross browser like this because your method won't work in IE: https://github.com/Wolfy87/Spark/blob/master/src/ajax.js#L3 – Olical Feb 02 '11 at 12:52
  • @cantbereached: what browser are you using? Does the Javascript console have anything useful to say? – Martijn Feb 02 '11 at 12:55
  • 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 – Oralet Feb 02 '11 at 13:03
  • Three questions: 1) does the Javascript console in Firefox say anything? 2) What does: http://jsbin.com/amano4 do? 3) Are you using any server-side code in your page (e.g. PHP)? – Martijn Feb 02 '11 at 13:10
  • 1) No 2) An alert box saying "Lines found: 3" and Hello World on the page 3) No – Oralet Feb 02 '11 at 13:17
  • 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 ``? – Martijn Feb 02 '11 at 13:24
  • 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 :/ – Oralet Feb 02 '11 at 13:32
  • Really weird. Your page isn't publicly available, by any chance? – Martijn Feb 02 '11 at 13:35
  • 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))`? – Martijn Feb 02 '11 at 13:41
  • I found it mate. I just used \r for newline instead of using \n or \r\n or /\n/. Thanks for your sincere help – Oralet Feb 02 '11 at 13:41