简体   繁体   中英

JavaScript Platform Independent Line separator

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

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

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.

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.

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/);

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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