JavaScript 平台独立行分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6833269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:01:16  来源:igfitidea点击:

JavaScript Platform Independent Line separator

javascriptnode.js

提问by tdashroy

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

System.getProperty("line.separator");JavaScript 中是否存在类似于 Java 的东西?

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

编辑:我使用的是非浏览器 JavaScript 环境 Node.js

回答by zemirco

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

我遇到了同样的问题,并遇到了这个相当古老的问题。经过一段时间的投资,我终于os.EOLos文档的最后找到了。

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

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

JSON.stringify在这种情况下很重要,否则您只会在控制台中看到一个空行(这是有道理的,因为它应该这样做)。在正常用例中,不需要它。

回答by T.J. Crowder

Not defined by the standard, no. Section 7.3defines 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.

没有由标准定义,没有。7.3 节为 JavaScript 程序的源代码定义了行终止符(有四个),但与平台对文本行终止符的定义没有任何关系。

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

如果您谈论的是非浏览器环境(shell 脚本、基于服务器的 JavaScript 等),我会查看您正在运行的环境(NodeJS、Rhino 等)的文档,这将希望为您提供那种环境信息。(当然,在 Rhino 中,您可以只使用 Java。)

If you're talking about browser-based environments, by and large, \nis 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 pagehas no carriage returns in it at all. And yet, if you run that page in Internet Explorer, note that it says it found \rcharacters 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 \ron its own.)

如果你谈论的是基于浏览器的环境中,由大\n被使用,但它是“由大”,可以咬你。:-) 有些浏览器甚至会textarea在 JavaScript 看到它们之前转换行结尾,例如在 a 中。例如,这个页面的原始版本根本没有回车。然而,如果您在 Internet Explorer 中运行该页面,请注意它说它\r在文本区域的值中找到了字符。不是 Chrome 或 Firefox(即使在 Windows 上运行时),但 Opera 也添加了它们(即使在 *nix 上运行时)。所以在浏览器上,我倾向于在访问多行字段值时“规范化”行尾,通过str = str.replace(/(?:\r\n|\r)+/g, "\n");,以防万一。(假设\r,无论是在其自身还是在其后\n, 应该是换行符;单独使用的旧 Mac \r。)

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\nconvertthe \non-the-fly when parsing the HTML string. Here's a live running copyyou can try for yourself with your favorite browser(s).

这工作,因为浏览器是使用\r\n转换\n解析HTML字符串时,在即时。这是一个实时运行的副本,您可以使用自己喜欢的浏览器亲自尝试。



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 \nfor 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.openfunction doesn't document the bflag from fopen(which is otherwise where it got that flags syntax), and if you look at the source, it sets _fmodeto _O_BINARYon MinGW. All of which leads me to suspect that files are only ever in binary mode. console.log's implementationuses \nfor newline, for what that's worth.

更新:啊,你正在使用 NodeJS。可悲的是,和你一样,我在 NodeJS 文档中根本没有看到任何关于此的内容。NodeJS 与 C/C++ 密切相关,在 C/C++ 中,\n无论操作系统如何,都可以使用换行符——在以文本模式打开的流上,它会即时转换为特定于操作系统的版本(读取和写入)。但是我在 NodeJS 中没有看到对文本模式文件流的任何支持。该fs.open函数不记录b来自的标志fopen(否则它是从那里获得标志语法的),如果您查看源代码,它在 MinGW 上设置_fmode_O_BINARY。所有这些都让我怀疑文件只处于二进制模式。console.log的实现使用\n了换行,什么是值得。

回答by AlienWebguy

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

有些浏览器使用\n,有些使用\r\n. 如果您想在换行符上拆分文本,请使用以下命令:

lines = foo.value.split(/\r\n|\r|\n/);

回答by shabunc

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.

不。RegExp 中有换行(\n)和回车(\r)字符,但没有像“line.separator”这样的属性。如果我们在谈论客户端 js。

回答by Billy Moon

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.

这是最好的回答......等待它...... StackOverflow!其中问题包括一个确定换行符的功能,此处链接的答案提供了一种检测平台的方法,然后推导出平台敏感换行符以供在 pre 或 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 \nhere: http://www.bennadel.com/blog/161-Ask-Ben-Javascript-Replace-And-Multiple-Lines-Line-Breaks.htm

此外,您可以找到一个很好的解释,说明在 pre 和 textarea 之外,JavaScipt 如何将所有行分隔符转换为\n这里:http: //www.bennadel.com/blog/161-Ask-Ben-Javascript-Replace-And-Multiple-换行符.htm

回答by Kevin

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

这对我有用(尽管我首先偶然发现了这个问题)。

JavaScript Platform Independent Line separator

JavaScript 平台独立行分隔符

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.

那里的问题集中在 Node.js 的使用上,但该答案很好地描述了跨浏览器和跨平台的情况。