简体   繁体   中英

(discord.js + document) Replace new lines with <br>

I am creating a ticket bot with discord.js. With this bot i am creating HTML transcripts that get uploaded to an apache web server.

I am trying to replace new lines (\\n) with <br> . The regex is right, but the html file isn't recognizing the <br> -s.

Code:

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

Result: https://imgur.com/a/MwXRfAe

It's because createTextNode does escape HTML characters, so it will escape your <br> tags.

You will need to find some other way, like using createElement s , or imply updating the innerHTML property:

 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! Officiis vero, enim fugiat porro architecto quod nam modi quasi natus at dolore deserunt quisquam ducimus. 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 ea illo doloribus? Assumenda cupiditate rem commodi dolores voluptatem sit fuga quam architecto perspiciatis.` } let descText = embed.description.replace(/(?:\\r\\n|\\r|\\n)/g, "<br>").replace(/"/g, "") let descNode = document.createElement('p') descNode.classList.add('embedDesc') descNode.innerHTML = `<p>${descText}</p>` document.querySelector('.embedContainer').appendChild(descNode)
 .embedContainer { border: 2px solid #eee; border-radius: 4px; color: #464646; font-family: sans-serif; margin: 0 auto; max-width: 400px; padding: 0 1rem; }
 <div class="embedContainer"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM