简体   繁体   English

JS中的换行符问题

[英]Newline \n problem in JS

I am reading a file with xmlHttp object and splitting the responseText with newlines by split method. 我正在使用xmlHttp对象读取文件,并使用split方法将responseText与换行符分开。

But "\\n" character literally doesn't work. 但“\\ n”字符确实不起作用。 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. 如果我将数组myPlaylist与其他字符分开,则没有错误。 Just \\n causes problem which I fail to understand. 只是\\ n导致我无法理解的问题。

At first, I thought the error was due to white-space:nowrap since I execute my code on Chrome. 起初,我认为错误是由于白色空间:nowrap,因为我在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. 同样,我在其他浏览器(Firefox,IE等)上尝试了我的代码,它也没有用。 Looks like I have a problem with using \\n. 看起来我在使用\\ 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. 顺便说一句,错误似乎是一个语法错误,因为它不只是忽略\\ n字符。 Simply causes my code not to work 只是导致我的代码不起作用

EDIT: An example responseText 编辑:一个示例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

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. 使用随机特殊字符后, \\ r \\ n字符用于回车工作就像我的问题的魅力。
It acted like a newline \\n character or at least it did its job in my case. 它的行为就像换行符\\ n或者至少它在我的情况下完成了它的工作。
Thanks everyone for answers and helpful comments. 感谢大家的回答和有用的评论。

Try using this line 尝试使用此行

/\n/

Instead of this one 而不是这一个

"\n"

Here's an SO thread that will provide a bit more insight 这是一个SO线程,将提供更多的洞察力

JavaScript string newline character? JavaScript字符串换行符?

EDIT1: I just tested this and it splits appropriately on new lines. 编辑1:我刚测试了这个,它在新线路上适当分割。 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: 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. 你能尝试将你的responseText转换成一个对象并看看你从中获得了什么 - 对不起,只是从这里开始拍摄,因为我没有时间嘲笑任何东西进行测试。

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 这是一篇可能对您有用的旧文章: http//www.peachpit.com/articles/article.aspx?p = 443580&seqNum = 4

This should work without problem. 应该没有问题。 Are you certain that myPlaylist has a responseText property, and that property is a string? 你确定myPlaylist有一个responseText属性,那个属性是一个字符串?

What happens if you catch an eventual error? 如果发现最终错误会怎样?

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

使用\\\\n而不是\\n我尝试了我的代码,它工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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