【发布时间】:2011-09-18 14:26:32
【问题描述】:
所以,我有一个远程检索到的文本文档,我想用 两个换行符 (\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 on 时,我看到了两个诗句之间的换行符。到现在为止还挺好。当我将其复制并粘贴到 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 进行拆分,它绝对可以正常工作。我尝试使用回车、换行符、将正则表达式作为分隔符传递,但似乎没有任何效果。我只是无法揭开这种行为的神秘面纱。
任何帮助将不胜感激。
【问题讨论】:
-
“回车”\n ...“换行”\r.
-
数据如何进入元素?也许某些东西缩进了 HTML 代码,导致
\n\n变成\n \n -
它确实有效:jsfiddle.net/7Xe7q c/p 那里的诗,然后单击拆分。然后检查控制台。
标签: javascript string split newline coffeescript