我有一个从远程检索的文本文件,我希望按照两个换行符(\n)分割它。例如,流行的赞美诗《万物亮丽莫名奇妙》。
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 分割,它完全正常工作。我已经尝试使用回车符、换行符、将正则表达式传递为分隔符,但什么都没有用。我无法澄清这种行为。
非常感谢任何帮助。
\n\n
变成了\n \n
。 - jpa