Smile0ff Smile0ff - 6 months ago 15
PHP Question

can't split text by double new line in js

I want to split this text(song) by double new line, i get it from php using ajax(fetch API)

You sad me goodbye
You did let me see
Then you[[em]] let me bye
So you let[[F]] me bye
know this can be
You know[[em]] I don't lie
This happened to me

Remember - we[[D]] knew
I can't fit my mind
I'm feeling[[C]] so blue
Why you telling «bye»
So you let me bye
I know[[F]] this can be
You know I don't lie
This happened to me[[A]]

This happened to me[[A]]
You sad me[[A]] goodbye
You did let me see
Then you let me[[A]] bye
So you let[[EM]] me bye
I know this can be
You know I don't lie
This happened to me[[A]]"


i had tried next regex's, but non of it doesn't work in my js

/\n{2,}/g
/\n\r{2,}/
/\r\n{2,}|\r{2,}|\n{2,}/g


here is the line of code that shows how i use it

let paragraphs = text.split(/\r\n{2,}|\r{2,}|\n{2,}/g);


However on https://regex101.com/ they all works fine, any help would be appreciated, thank you!

P.S. headers in PHP already checked twice(JSON and UTF-8).

Answer
text.split(/\n\s*\n/)

gives me next

[
"You sad me goodbye
You did let me see
Then you[[em]] let me bye
So you let[[F]] me bye
know this can be
You know[[em]] I don't lie
This happened to me", // <--
"Remember - we[[D]] knew
I can't fit my mind
I'm feeling[[C]] so blue
Why you telling «bye»
So you let me bye
I know[[F]] this can be
You know I don't lie
This happened to me[[A]]", // <--
"This happened to me[[A]]
You sad me[[A]] goodbye
You did let me see
Then you let me[[A]] bye
So you let[[EM]] me bye
I know this can be
You know I don't lie
This happened to me[[A]]""
]

text.split(/\n\s*\n/).length // 3

about your code

text.charCodeAt(151) // 10
text.charCodeAt(152) // 82 <--
'\n'.charCodeAt(0) // 10
'\r'.charCodeAt(0) // 13 <--