特定于平台的新行的 Node.JS 常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10864486/
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-09-02 15:49:05  来源:igfitidea点击:

Node.JS constant for platform-specific new line?

node.js

提问by Brad

Is there a constant available in Node.JS for a newline character that is specific to the platform the application is running on?

Node.JS 中是否有一个常量可用于特定于应用程序运行平台的换行符?

For example:

例如:

  • Windows: \r\n
  • *nix: \n
  • 视窗: \r\n
  • *尼克斯: \n

回答by Will Munn

Not sure if this is new in the 0.8.x but there is now a constant http://nodejs.org/api/os.html#os_os_eol

不确定这是否是 0.8.x 中的新内容,但现在有一个不变的http://nodejs.org/api/os.html#os_os_eol

var endOfLine = require('os').EOL;

回答by Saul

Node.js 0.6.x and earlier:

Node.js 0.6.x 及更早版本:

Unfortunately there isn't a constant, but you determine it yourself using:

不幸的是,没有常数,但您可以使用以下方法自行确定:

var nl = (process.platform === 'win32' ? '\r\n' : '\n')

(note this is quite a naive solution)

(请注意,这是一个非常幼稚的解决方案)