gpt4 book ai didi

javascript - 奇怪的 JavaScript 换行符拆分行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:28 24 4
gpt4 key购买 nike

所以,我有一个远程检索的文本文档,我想用两个换行符 (\n) 拆分它。例如,流行的赞美诗 All Things Bright And Beautiful

All Things Bright And Beautiful
Cecil F. Alexander

All things bright and beautiful,
All creatures great and small,
All things wise and wonderful,
The Lord God made them all.

Each little flower that opens,
Each little bird that sings,
He made their glowing colours,
He made their tiny wings.

The purple-headed mountain,
The river running by,
The sunset and the morning,
That brightens up the sky;

The cold wind in the winter,
The pleasant summer sun,
The ripe fruits in the garden,
He made them every one;

The tall trees in the greenwood,
The meadows for our play,
The rushes by the water,
To gather every day;

He gave us eyes to see them,
And lips that we might tell
How great is God Almighty,
Who has made all things well.

在 TextMate 中查看带有 invisibles 的内容,我看到在两节之间有两个换行符。到目前为止,一切都很好。当我将其复制并粘贴到 irb 时,一切正常:

ruby-1.8.7-p334 :034 > content.split "\n\n"
=> ["All Things Bright And Beautiful\nCecil F. Alexander", "All things bright and beautiful,\nAll creatures great and small,\nAll things wise and wonderful,\nThe Lord God made them all. ", "Each little flower that opens,\nEach little bird that sings,\nHe made their glowing colours,\nHe made their tiny wings.", "The purple-headed mountain,\nThe river running by,\nThe sunset and the morning,\nThat brightens up the sky;", "The cold wind in the winter,\nThe pleasant summer sun,\nThe ripe fruits in the garden,\nHe made them every one;", "The tall trees in the greenwood,\nThe meadows for our play,\nThe rushes by the water,\nTo gather every day;", "He gave us eyes to see them,\nAnd lips that we might tell\nHow great is God Almighty,\nWho has made all things well."]
ruby-1.8.7-p334 :035 >

当我使用 JavaScript 时,问题就来了。在控制台中:

enter image description here

如果我将 一个\n 分开,它绝对可以正常工作。我试过使用回车符、换行符、传递正则表达式作为分隔符,但似乎没有任何效果。我无法揭开这种行为的神秘面纱。

如有任何帮助,我们将不胜感激。

最佳答案

Javascript 的拆分与 Ruby 的拆分方式相同。由于您将文本导入脚本的方式,\n 很可能变成了其他内容。

做一个this.element_content.split('')

var s = this.element_content;
var chars = [];
for(i=0; i<s.length; i++){ chars.push(s.charAt(i)); }
console.log(chars)

将您的字符串转换为字符数组并检查换行符中实际使用的内容。

关于javascript - 奇怪的 JavaScript 换行符拆分行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462104/

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