0

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

4

1 回答 1

2

That's not JavaScript related. 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. /r is for DOS ,/r/n is for FreeBSD based(Mac). This will enforce a single style for all your new lines, the Unix default style which is \n.

于 2012-12-01T10:06:21.587 回答