2

I have a long paragraph from which I wanted to split using the following pattern [a-z,A-Z,0-9]\n[a-z,A-Z,0-9]\n[a-z,A-Z,0-9]\n.i.e I want to make a split based on few sentence followed by new line(the sequence should be repeated thrice and then split should be made). I am not getting how can I write this sequence inside the split function. Please help.

Starting text: 'Hello World! The decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.\n There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped. \n Building paragraphs can be like building a skyscraper: there must be a well-planned foundation that supports what you are building.\n Any cracks, inconsistencies, or other corruptions of the foundation can cause your whole paper to crumble.\nThe decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.\nThere are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped.

I want to split the above paragraph into 2 sections.

First section should be:

Hello World!The decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped. \n Building paragraphs can be like building a skyscraper: there must be a well-planned foundation that supports what you are building.

Second section should be:

Any cracks, inconsistencies, or other corruptions of the foundation can cause your whole paper to crumble.The decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped.

james
  • 110
  • 1
  • 11
Priyanka
  • 67
  • 1
  • 8

1 Answers1

1

From what i understand, you want to split your text into match of three lines ? I've built a regex that does that here.

([a-zA-Z! ,;“”.\-:]+(\n|$)){3}

You can test more case in this regex101

Here is how to use it:

let string = `Hello World!The decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.
 There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped. 
 Building paragraphs can be like building a skyscraper: there must be a well-planned foundation that supports what you are building.
Any cracks, inconsistencies, or other corruptions of the foundation can cause your whole paper to crumble.
The decision about what to put into your paragraphs begins with the germination of a seed of ideas; this “germination process” is better known as brainstorming.
There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped.`;
let array = string.match(/([a-zA-Z! ,;“”.\-:]+(\n|$)){3}/g);
console.log(array);
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • How can I make use of this regular expression in splitting?Help me with the syntax since I am new to regular expression. – Priyanka Nov 14 '19 at 13:27
  • @Priyanka see [this answers](https://stackoverflow.com/a/37838618/5784924) – Nicolas Nov 14 '19 at 13:30
  • It's giving wrong output. ) [ "", " Building paragraphs can be like building a skyscraper: there must be a well-planned foundation that supports what you are building. ", " ", "", "There are many techniques for brainstorming; whichever one you choose, this stage of paragraph development cannot be skipped", "", "" ] – Priyanka Nov 14 '19 at 13:37
  • @Priyanka My bad, i've edited my answer with a complete example. – Nicolas Nov 14 '19 at 14:01