简体   繁体   English

对于JavaScript,“\\ n”在string.split中不起作用吗?

[英]Is “\n” not working in string.split for JavaScript?

I have a external resource (raw text file) like: 我有一个外部资源(原始文本文件),如:

1. Title 1
Content 1
|
2. Title 2
Content 2
|
3. Title 3
Content 3

I load them in a web page via AJAX and want to use content.split("\\n|\\n") to extract its content into an array for my use. 我通过AJAX将它们加载到网页中,并希望使用content.split(“\\ n | \\ n”)将其内容提取到一个数组供我使用。 But it seems it's not working. 但它似乎无法正常工作。

What's weird is that split("\\n|") works, but neither do split("|\\n") nor split("\\n|\\n") work. 奇怪的是split(“\\ n |”)有效,但split(“| \\ n”)split(“\\ n | \\ n”)都不起作用。

That's not JavaScript related. 这与JavaScript无关。 That's related to the operating system. 这与操作系统有关。

  var canonicalizeNewlines = function(str) {
      return str.replace(/(\r\n|\r|\n)/g, '\n');
  };

Basically DOS(Windows), FreeBSD and Unix systems use different white characters to end a line. 基本上DOS(Windows),FreeBSD和Unix系统使用不同的白色字符来结束一行。 /r is for DOS , /r/n is for FreeBSD based(Mac). /r用于DOS, /r/n用于基于FreeBSD(Mac)。 This will enforce a single style for all your new lines, the Unix default style which is \\n . 这将为所有新行强制执行单一样式,Unix默认样式为\\n

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

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