Newline \ n problem in JS

I read the file with the object xmlHttpand split it responseTextinto new lines by the split method.

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

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

There is no error if I split the myPlaylist array into other characters. Just \ n causing a problem that I don't understand.

At first, I thought the error was related to white space: nowrap, since I am executing my code in Chrome. Although I never used white space anywhere, I tried to set it to normal, but that didn't work. Similarly, I tried my code in other browsers (Firefox, IE, etc.), It didn’t work either. Looks like I'm having a problem using \ n. Is there any other way to use a new line or an error with my code?

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

EDIT: ExampleresponseText

[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
+3
source share
5 answers

.
\r, , .
\n , , .
.

+3

/\n/

"\n"
+1

SO,

JavaScript?

EDIT1: , . , ?

<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:

Text , - , , - .

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

Here are some old articles you might find useful: http://www.peachpit.com/articles/article.aspx?p=443580&seqNum=4

0
source

This should work without problems. Are you sure it myPlaylisthas a property responseText, and this property is a string?

What happens if you catch a possible mistake?

try {
  var lines = myPlaylist.responseText.split(/\n/g);
  alert(lines.length);
} catch (e) {
  alert(e.message);
}
0
source

Use \\ninstead \n, I tried my code and it works great

-2
source

Source: https://habr.com/ru/post/1789487/


All Articles