Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on blank lines

I like to use Javascript's split method to split a string like the one below into an array on every blank white line.

How can I accomplish this. Splitting on \n doesn't because I'd like each paragraph to occupy just one element of the array and there are multiple \n's in each paragraph.

Any help will be greatly appreciated!

String:

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.
like image 732
Casey Avatar asked May 04 '16 00:05

Casey


People also ask

How do you split a string by a blank line?

To split text by empty line, split the string on two newline characters, e.g. my_str. split('\n\n') for POSIX encoded files and my_str. split('\r\n\r\n') for Windows encoded files.

What happens when split empty string?

If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

How do you split a string around a space?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do I split a string without a separator?

To split a string without removing the delimiter: Use the str. split() method to split the string into a list.


1 Answers

Try something like this:

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);
like image 78
Eduardo Cuomo Avatar answered Oct 10 '22 08:10

Eduardo Cuomo