【问题标题】:Bizarre JavaScript newline splitting behaviour奇怪的 JavaScript 换行符拆分行为
【发布时间】: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


【解决方案1】:

Javascript 的拆分与 Ruby 的拆分工作方式相同。 \ns 很可能由于您将文本导入脚本的方式而更改为其他内容。

做一个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)

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

【讨论】:

  • 当拆分为单个字符时,我可以看到一个换行符,然后是一个空字符串(没有空格,只有“”),然后是另一个换行符。我不知道如何将其“转换”为 split() 调用。
  • 其中一位 cmet 提到它可能是split('\n \n')。我刚刚检查过,这可能是你想要的。 (事后看来,在这种情况下, split('') 可能不是转换为数组的好方法。普通的 for 循环和 .charAt 方法可能更精确)
  • 非常感谢您迄今为止的帮助。不幸的是,split('\n \n') 没有解决任何问题。我也试过用双引号(没关系)。这真是让人头疼!
  • 很可能是回车问题。也许删除所有回车的正则表达式也会有帮助?
  • @Jamie Rumbelow:您是否使用更准确的转换来检查空格中使用了哪些字符?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 2014-01-11
    • 2016-03-07
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    相关资源
    最近更新 更多