简体   繁体   中英

Bizarre JavaScript newline splitting behaviour

So, I've got a remotely retrieved text document that I want to split by two newline (\\n) characters . For example, the popular hymn 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.

Taking a look in TextMate with invisibles on shows me two newline characters in between verses. So far so good. When I copy and paste this into irb everything works absolutely fine:

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 > 

The problem comes the moment I go to JavaScript. In the console:

在此输入图像描述

If I split by one \\n it works absolutely fine. I've tried using carriage returns, newlines, passing a regex through as the delimiter and nothing seems to be working. I just cannot demystify this behaviour.

Any help would be thoroughly appreciated.

Javascript's split works the same as Ruby's. Most likely the \\n s changed into something else because of how you imported the text to your script.

Do a 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)

to convert your string into an array of characters and check what is actually being used in the line breaks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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