我想用双新行拆分这段文本(歌曲),我使用 ajax(fetch API)从 php 获取它
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]]"
我试过下一个正则表达式,但没有一个在我的 js 中不起作用
/\n{2,}/g
/\n\r{2,}/
/\r\n{2,}|\r{2,}|\n{2,}/g
这是显示我如何使用它的代码行
let paragraphs = text.split(/\r\n{2,}|\r{2,}|\n{2,}/g);
然而在https://regex101.com/他们都工作正常,我们将不胜感激,谢谢!
附言PHP 中的 header 已经检查了两次(JSON 和 UTF-8)。
最佳答案
text.split(/\n\s*\n/)
给我下一个
[
"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
关于你的代码
text.charCodeAt(151) // 10
text.charCodeAt(152) // 82 <--
'\n'.charCodeAt(0) // 10
'\r'.charCodeAt(0) // 13 <--
关于javascript - 无法通过js中的双换行符拆分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37513550/