2016-05-04 8 views
1

私は、以下のような文字列を空白の白線ごとに分割するために、Javascriptの分割方法を使いたいと思っています。ブランクの文字列を分割する

どうすれば実現できますか?それぞれの段落が配列の1つの要素を占有し、各段落に複数の\ 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. 

答えて

2

はちょうど '\ N \ N' で分割し、各段落の間に改行があると仮定。

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

EDIT:この場合

、 '& #xd'、またはキャリッジリターンを使用https://raw.githubusercontent.com/NinjaBanjo/synonym-search/master/out/w00020.html分割線でのテキストのソース。このテキストを取得してtext.split('
\n
')で分割した後、配列は正しいサイズになります。作業例はhttp://jsfiddle.net/cuVdE/211/です。

+0

私はそれを試してみましたが、私は動作しませんでした。私のarray.lengthは1だけでした。使用しているファイルの1つへのリンクがあります:https://raw.githubusercontent.com/NinjaBanjo/synonym-search/master/out/w00020.html – Casey

+0

あなたのコードを投稿できますか?そのウェブサイトのテキストを取得していますか? – MattCorr

+0

http://jsfiddle.net/cuVdE/208/を使用して、私はあなたがそのウェブサイトで見ている改行が実際には&#xd、つまりキャリッジリターンであると判断しました。私はこの文字列に沿って分割し、それは働いた。 http://jsfiddle.net/cuVdE/210/の作業例を参照してください。私は私の答えを更新します。 – MattCorr

0

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)); 

} 
1

はこのような何か試しください:Windowsでは

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

を:

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

Linuxの場合:Macの

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

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