3

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)

1
  • What have you tried so far? A solution to this should be easily searchable. For starters, stackoverflow.com/questions/8125709/… is the first result when searching "javascript split by newline". Commented May 17, 2021 at 15:24

4 Answers 4

3

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

You can split it on \n\n.

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('\n\n') //split up
//logging every new line:
split.forEach(function(item){console.log(item)})

Sign up to request clarification or add additional context in comments.

Comments

3

You could split on CR?LF occurring one or more times:

var input = "Hey is some text some text?\r\n\r\nYou 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\r\n\r\nMy 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";
var paragraphs = input.split(/(?:\r?\n)+/);
console.log(paragraphs);

Comments

2

You could just use split() method and split it by \n\n. \n Means new line, on your text, there is 2 new line, hence we use \n\n.

const 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`;

const splittedText = text.split("\n\n");
console.log(splittedText);

Comments

0

Sometimes, spacing is an issue in text from user-generated content. This implementation parses and removes superfluous spacing.

const 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`;

const wrappedText = text
  .split('\n')
  .filter(v => v.trim())
  .map(content => `<p>${content.trim()}</p>`)
  .join('\n');
  
console.log(wrappedText);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.