33

Does something similar to Java's System.getProperty("line.separator"); exist in JavaScript?

Edit: I am using the non-browser JavaScript environment Node.js

FK-
  • 1,462
  • 1
  • 10
  • 17
tdashroy
  • 1,405
  • 1
  • 19
  • 18
  • 1
    Possible duplicate of [How to determine the OS path separator in JavaScript?](http://stackoverflow.com/questions/125813/how-to-determine-the-os-path-separator-in-javascript) – Ciro Santilli OurBigBook.com Nov 01 '16 at 13:44
  • 1
    The path separator is either a colon or semi-colon. It is not the same thing as the new line separator. See https://stackoverflow.com/questions/5971964/file-separator-or-file-pathseparator – Stephen Ostermiller Nov 01 '16 at 14:54
  • The path separator is either a slash or a backslash. But the line separator could be "\n", "\r\n", or "\r". So it's not duplicated. – boris1993 Jul 09 '20 at 09:57

6 Answers6

47

I had the same problem and came across this quite old question. After investing for some time I finally found os.EOL at the very end of the os documentation.

var os = require('os')
console.log(JSON.stringify(os.EOL)) // prints "\n" on my Mac

JSON.stringify is in this case important, otherwise you would only see a blank line in the console (which makes sense because that's what it should do). In an normal use case it is not needed.

zemirco
  • 16,171
  • 8
  • 62
  • 96
  • 1
    No idea if this was there when I asked the question, but this definitely seems to be the correct answer now. Thanks for helping out! – tdashroy Sep 17 '13 at 00:11
  • 2
    @tdashroy I would like to add to the answer, if you are processing files that not necessarily come from the same OS, you can always use regular expresions in most text manipulation functions such as `String.split()`, `String.replace()`, etc... Use `/\r\n|\n/` for that. –  Jun 05 '14 at 15:52
19

Not defined by the standard, no. Section 7.3 defines line terminators for the source code of a JavaScript program (there are four), but nothing related to the platform's definition of a line terminator for text.

If you're talking about non-browser environments (shell scripts, server-based JavaScript, etc.), I'd look at the documentation for the environment in which you're running (NodeJS, Rhino, etc.), which will hopefully have that kind of environmental info for you. (In Rhino, of course, you can just use Java's.)

If you're talking about browser-based environments, by and large, \n is used, but it's that "by and large" that can bite you. :-) Some browsers will even convert line endings, such as in a textarea, before JavaScript even sees them. For instance, the raw version of this page has no carriage returns in it at all. And yet, if you run that page in Internet Explorer, note that it says it found \r characters in the text area's value. Not so Chrome or Firefox (even when running on Windows), but Opera adds them too (even when running on *nix). So on browsers, I tend to "normalize" line endings when accessing multi-line field values, via str = str.replace(/(?:\r\n|\r)+/g, "\n");, just in case. (That assumes that \r, both on its own and when followed by \n, should be a line break; older Macs used \r on its own.)

Continuing with the browser side of things, since different browsers do different things even on the same OS, is there any way to know what they use? Why, yes there is, you can find out with a sneaky trick:

function getLineBreakSequence() {
    var div, ta, text;

    div = document.createElement("div");
    div.innerHTML = "<textarea>one\ntwo</textarea>";
    ta = div.firstChild;
    text = ta.value;
    return text.indexOf("\r") >= 0 ? "\r\n" : "\n";
}

This works because the browsers that use \r\n convert the \n on-the-fly when parsing the HTML string. Here's a live running copy you can try for yourself with your favorite browser(s).


Update: Ah, you're using NodeJS. Sadly, like you, I don't see anything at all about this in the NodeJS docs. NodeJS is very closely tied to C/C++, and in C/C++, you use \n for linebreak regardless of the OS — on streams opened in text mode it gets converted to the OS-specific version on the fly (both read and write). But I don't see any support for text-mode file streams in NodeJS. The fs.open function doesn't document the b flag from fopen (which is otherwise where it got that flags syntax), and if you look at the source, it sets _fmode to _O_BINARY on MinGW. All of which leads me to suspect that files are only ever in binary mode. console.log's implementation uses \n for newline, for what that's worth.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Ah, I should have clarified, I am indeed talking about a non-browser environment. I am using NodeJS. I will try looking in the documentation for it, I had not thought about that yet (I'm pretty new to this if you couldn't tell). Other than that, thanks for all the great info! – tdashroy Jul 26 '11 at 17:32
  • Great answer! Where's the +2 button? – Sean Vieira Jul 26 '11 at 17:45
  • I couldn't find anything looking in the documentation, so it appears the answer is no (either that or I'm not looking in the right spot). Thanks for your help! – tdashroy Jul 26 '11 at 18:41
12

Some browsers use \n and some use \r\n. If you want to split the text on newlines just use this:

lines = foo.value.split(/\r\n|\r|\n/);
pyrospade
  • 7,870
  • 4
  • 36
  • 52
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1

Nope. There are linefeed (\n) and carriage return (\r) characters in RegExp, but there is no such a thing like "line.separator" property. If we are talking about clientside js.

shabunc
  • 23,119
  • 19
  • 77
  • 102
0

This did it for me (though I stumbled onto this question first).

JavaScript Platform Independent Line separator

function getLineBreakSequence() {
    var div, ta, text;

    div = document.createElement("div");
    div.innerHTML = "<textarea>one\ntwo</textarea>";
    ta = div.firstChild;
    text = ta.value;
    return text.indexOf("\r") >= 0 ? "\r\n" : "\n";
}

The question there is focused on Node.js use, but that answer describes the cross browser and cross platform situation well.

Community
  • 1
  • 1
Kevin
  • 2,234
  • 2
  • 21
  • 26
0

This is best answered on ... wait for it ... StackOverflow! Where the question includes one function to determine line break character, and the answer linked here provides a way to detect platform, and then deduce platform sensitive line break character for use within pre or textarea.

In the browser, how does one determine which flavour of line breaks is appropriate to the OS?

Also, you can find a good explanation of how outside a pre and textarea, JavaScipt converts all line separators to \n here: http://www.bennadel.com/blog/161-Ask-Ben-Javascript-Replace-And-Multiple-Lines-Line-Breaks.htm

Community
  • 1
  • 1
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • Sorry about that, I should have clarified (I made an edit above) I am using the non-browser JavaScript environment called Node.js – tdashroy Jul 26 '11 at 17:36