Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在空行上拆分字符串_Javascript_Arrays - Fatal编程技术网

Javascript 在空行上拆分字符串

Javascript 在空行上拆分字符串,javascript,arrays,Javascript,Arrays,我喜欢使用Javascript的split方法将下面这样的字符串拆分为每一条空白白线上的数组 我怎样才能做到这一点。“在上拆分”不正确,因为我希望每个段落只占用数组的一个元素,并且每个段落中有多个\n 任何帮助都将不胜感激 字符串: CHAPTER TWENTY-ONE. MARIE'S SIDE OF IT CHAPTER TWENTY-TWO. THE CURE COMPLETE CHAPTER ONE. THE FEVER MANIFESTS ITSELF There is a

我喜欢使用Javascript的split方法将下面这样的字符串拆分为每一条空白白线上的数组

我怎样才能做到这一点。“在上拆分”不正确,因为我希望每个段落只占用数组的一个元素,并且每个段落中有多个\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.

假设每个段落之间有换行符,只需用“\n\n”分隔即可

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


在本例中,使用“ ”或回车符分隔行处的文本源。获取此文本并使用
text.split('
;\n
;')
将其拆分后,数组大小正确。工作示例位于。

\n{2,}搜索2个或更多\n并全局执行/g


试着这样做:

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

我试过了,但没有成功。我的数组长度只有1。下面是我正在使用的一个文件的链接:你能发布你的代码来获取该网站的文本吗?通过使用,我确定你在该网站上看到的换行符实际上是“ ”或回车符。我沿着这根绳子劈开,它成功了。请参阅上的工作示例。我还将更新我的答案。哎呀,刚刚意识到“ \n ”效果更好。更新于
var paragraphs = text.split(/\r{2,}/g);