Skip to content
Advertisement

How to split text in javascript into paragraphs?

I have the following piece of text:

“Hey is some text some text?

You are some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text

My name is some text some text some text some text some text some text some text some text some text some text some text some text some text some text”

I need split in into paragraph and get array from 3 element(I have 3 paragraps above)

Advertisement

Answer

that is quite simple. you use the String.prototype.split function on your string.

You can split it on nn.

let text=`Hey is some text some text?

You are some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text

My name is some text some text some text some text some text some text some text some text some text some text some text some text some text some text`//declare variable
let split=text.split('nn') //split up
//logging every new line:
split.forEach(function(item){console.log(item)})
Advertisement