(discord.js+文档)将新行替换为< br>

d4so4syb  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(340)

我正在使用discord.js创建一个票证机器人。通过这个bot,我创建了上传到ApacheWeb服务器的html文本。
我正在尝试将新行(\n)替换为 <br> . 正则表达式是正确的,但html文件无法识别 <br> -美国。
代码:

let descText = document.createTextNode(embed.description.replace(/(?:\r\n|\r|\n)/g, "<br>").replace(/"/g, ""));
descNode.append(descText);

结果:https://imgur.com/a/mwxrfae

fafcakar

fafcakar1#

因为 createTextNode 不转义html字符,因此它将转义 <br> 标签。
你需要找到其他的方法,比如使用 createElement s、 或者暗示更新 innerHTML 财产:

let embed = {
  description: `Title

Lorem ipsum dolor sit amet consectetur adipisicing elit. Labore vitae quidem maiores dicta cum? Tempora ex numquam qui rerum, earum maiores cupiditate! Reiciendis ea sit ipsam veritatis nemo, voluptas nam?

Officiis vero, enim fugiat porro architecto quod nam modi quasi natus at dolore deserunt quisquam ducimus corrupti nesciunt, dolor, iusto exercitationem. Minima impedit explicabo consequatur sint quibusdam, minus nam asperiores.

Ut impedit asperiores quo beatae quidem aspernatur soluta sequi sapiente, sit illo commodi itaque temporibus facere dolorem vero? Explicabo consequatur optio sunt sequi dolor inventore nobis asperiores ea illo doloribus?

Assumenda cupiditate rem commodi dolores voluptatem sit fuga quam architecto perspiciatis est in aspernatur quaerat ad, magnam tempora nobis neque animi pariatur voluptate, vitae consequuntur totam? Magnam consequatur porro distinctio.`
}

let descNode = document.querySelector('div')
let descText = embed.description.replace(/(?:\r\n|\r|\n)/g, "<br>").replace(/"/g, "")
descNode.innerHTML += `<p>${descText}</p>`
<div></div>

相关问题