javascript - 无法通过js中的双换行符拆分文本

标签 javascript php

我想用双新行拆分这段文本(歌曲),我使用 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/

相关文章:

javascript - 在 Safari 中,我认为 IE7 和 8 Math.random() 也不是随机的?

javascript - 如何分别更改每个图标上单击时的颜色? React,样式组件

javascript - 仅识别系统

php - preg_match_all 空匹配

javascript - 无法在 Electron 的渲染器过程中使用事件监听器

javascript - 更新 Stockchart y 轴标签更改位置和标签精度

javascript - 在django中显示 "still loading"

php - Symfony 2/Doctrine 2 : Two Entities for the same table, 用一个代替另一个

php - Jquery 自动完成中的通知

php - Firebase 索引.php