gpt4 book ai didi

javascript - JS 字符串中的行尾(也称为换行符)

转载 作者:行者123 更新时间:2023-11-29 18:54:24 25 4
gpt4 key购买 nike

众所周知,类 Unix 系统使用 LF 字符作为换行符,而 Windows 使用 CR+LF

但是,当我在我的 Windows PC 上从本地 HTML 文件测试这段代码时,似乎 JS 将所​​有换行符视为用 LF 分隔。这是正确的假设吗?

var string = `
foo




bar
`;

// There should be only one blank line between foo and bar.

// \n - Works
// string = string.replace(/^(\s*\n){2,}/gm, '\n');

// \r\n - Doesn't work
string = string.replace(/^(\s*\r\n){2,}/gm, '\r\n');

alert(string);

// That is, it seems that JS treat all newlines as separated with
// `LF` instead of `CR+LF`?

最佳答案

我想我找到了一个解释。

您正在使用 ES6 Template Literal构造您的多行字符串。

根据ECMAScript specs一个

.. template literal component is interpreted as a sequence of Unicodecode points. The Template Value (TV) of a literal component isdescribed in terms of code unit values (SV, 11.8.4) contributed by thevarious parts of the template literal component. As part of thisprocess, some Unicode code points within the template component areinterpreted as having a mathematical value (MV, 11.8.3). Indetermining a TV, escape sequences are replaced by the UTF-16 codeunit(s) of the Unicode code point represented by the escape sequence.The Template Raw Value (TRV) is similar to a Template Value with thedifference that in TRVs escape sequences are interpreted literally.

在此之下,定义为:

The TRV of LineTerminatorSequence::<LF> is the code unit 0x000A (LINEFEED).
The TRV of LineTerminatorSequence::<CR> is the code unit 0x000A (LINE FEED).

我的解释是,当您使用模板文字时,您总是只得到一个换行符 - 无论操作系统特定的换行符定义如何。

最后,在JavaScript's regular expressions一个

\n matches a line feed (U+000A).

它描述了观察到的行为。

但是,如果您定义字符串文字 '\r\n' 或从文件流中读取文本等,其中包含特定于操作系统的换行符,则您必须处理它。

下面是一些演示模板文字行为的测试:

`a
b`.split('')
.map(function (char) {
console.log(char.charCodeAt(0));
});

(String.raw`a
b`).split('')
.map(function (char) {
console.log(char.charCodeAt(0));
});

'a\r\nb'.split('')
.map(function (char) {
console.log(char.charCodeAt(0));
});

"a\
b".split('')
.map(function (char) {
console.log(char.charCodeAt(0));
});

解释结果:
char(97) = a, char(98) = b
char(10) = \n, char(13) = \r

关于javascript - JS 字符串中的行尾(也称为换行符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49968130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com