繁体   English   中英

奇怪的JavaScript换行分裂行为

[英]Bizarre JavaScript newline splitting behaviour

所以,我有一个远程检索的文本文档,我希望用两个换行符(\\ 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,可以看到两节之间有两个换行符。 到现在为止还挺好。 当我将其复制并粘贴到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的那一刻。 在控制台中:

在此输入图像描述

如果我分开一个\\ 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)

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

试试, this.element_content.split(/[\\n\\r]{1,2}/m)使用m表示多行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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