簡體   English   中英

在空白行上分割字符串

[英]Split string on blank lines

我喜歡使用Javascript的split方法在每個空白行上將如下所示的字符串拆分為一個數組。

我該怎么做。 在\\ n上分割並不是因為我希望每個段落僅占據數組的一個元素,並且每個段落中有多個\\ n。

任何幫助將不勝感激!

串:

CHAPTER TWENTY-ONE. MARIE'S SIDE OF IT

CHAPTER TWENTY-TWO. THE CURE COMPLETE



CHAPTER ONE. THE FEVER MANIFESTS ITSELF

There is a certain malady of the mind induced by too much of one
thing. Just as the body fed too long upon meat becomes a prey to that
horrid disease called scurvy, so the mind fed too long upon monotony
succumbs to the insidious mental ailment which the West calls "cabin
fever." True, it parades under different names, according to
circumstances and caste. You may be afflicted in a palace and call it
ennui, and it may drive you to commit peccadillos and indiscretions of
various sorts. You may be attacked in a middle-class apartment house, and
call it various names, and it may drive you to cafe life and affinities
and alimony. You may have it wherever you are shunted into a backwater of
life, and lose the sense of being borne along in the full current of
progress. Be sure that it will make you abnormally sensitive to little
things; irritable where once you were amiable; glum where once you went
whistling about your work and your play. It is the crystallizer of
character, the acid test of friendship, the final seal set upon enmity.
It will betray your little, hidden weaknesses, cut and polish your
undiscovered virtues, reveal you in all your glory or your vileness to
your companions in exile--if so be you have any.

If you would test the soul of a friend, take him into the wilderness
and rub elbows with him for five months! One of three things will surely
happen: You will hate each other afterward with that enlightened hatred
which is seasoned with contempt; you will emerge with the contempt tinged
with a pitying toleration, or you will be close, unquestioning friends to
the last six feet of earth--and beyond. All these things will cabin fever
do, and more. It has committed murder, many's the time. It has driven men
crazy. It has warped and distorted character out of all semblance to its
former self. It has sweetened love and killed love. There is an
antidote--but I am going to let you find the antidote somewhere in the
story.

嘗試這樣的事情:

var paragraphs = text
    .replace(/\n\r/g, "\n")
    .replace(/\r/g, "\n")
    .split(/\n{2,}/g);

在Windows上:

var paragraphs = text.split(/(\n\r){2,}/g);

在Linux上:

var paragraphs = text.split(/\n{2,}/g);

在Mac上:

var paragraphs = text.split(/\r{2,}/g);

假設每個段落之間都有一個換行符,只是用'\\ n \\ n'分隔。

var paragraphs = text.split('\n\n');

編輯:

在這種情況下,位於https://raw.githubusercontent.com/NinjaBanjo/synonym-search/master/out/w00020.html的文本源使用'&#xd'或回車符分隔行。 獲得此文本並使用text.split('
\\n
')分割后,數組的大小正確。 http://jsfiddle.net/cuVdE/211/上的工作示例。

\\ n {2,}搜索2個或更多\\ n並全局執行/ g

http://jsfiddle.net/LZf7H/3574/

   var regex = /\n{2,}/g;

function getValue()  {
    return document.getElementById("myinput").value;
}



function match() {
     console.log(getValue().split(regex)); 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM