0

Using NodeJS and ExpressJS, im passing in the data from MySQl it has new lines but not when i show it on my HTML pages, heres proof it shows new lines (random text)

the text

results from database

the html page

and this is the code when rending in the html

<span><%=userRoles[0].description%></span>

i need it to add the new lines of its in the row

vector17
  • 13
  • 4
  • Q: Do you know that "\r\n" newlines in your HTML text will *NOT* appear as line breaks when you view them in the browser? Your two options to display linefeeds in HTML are !) use the [
    ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre) tag,or 2) to a text substitution of `\r\n` to `
    \r\n`.
    – paulsm4 Dec 08 '19 at 03:06

1 Answers1

0

Method 1: replace all line breaks(\r\n,\r,\n) with <br> tag

You need to use <%- code %> instead of <%= code %> to avoid the html escape.

<span><%- userRoles[0].description.replace(/(?:\r\n|\r|\n)/g, '<br>'); %></span>

Method 2: use <pre> tag

the <pre> element will display the line breaks

<pre><%- userRoles[0].description %></pre>
gokaka
  • 136
  • 1
  • 5