javascript - 在空行上拆分字符串

标签 javascript arrays

我想使用 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.

最佳答案

尝试这样的事情:

const paragraphs = text
  // Split on new line
  .split(/[\n\r]+/g)
  // Remove blank lines
  .filter((p) => p.trim());

const text = `
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.
`;

const paragraphs = text.split(/[\n\r]+/g).filter((p) => p.trim());

console.info('Paragraphs:', paragraphs);

关于javascript - 在空行上拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37016320/

相关文章:

javascript - 字符串中的所有字符都必须匹配正则表达式

javascript - Firefox 30.0 中的 ExtJS 3.4 错误

javascript - CQ5 AEM : how to get a component by name within a dialog using javascript

java - 将实际输入值存储到数组中以便与另一个数组进行比较

javascript - 通过嵌套的JSON数组对象nodejs解析

javascript - 你如何放置上传文件的引用并能够以 HTML 打印文件的值?

javascript - 来自服务器的 Meteor 发布信息更改了 DOM 并删除了元素上的键盘焦点

Java数组初始化

c++ - std::array 的嵌套聚合初始化

java - 在另一个类中使用一个类的对象和方法数组错误 - JAVA