How to Convert Text to HTML Using JavaScript?

Estimated read time 1 min read

Here is an example of how you can convert text to HTML using JavaScript:

function convertTextToHTML(text) {
  return text.replace(/\n/g, "<br>");

const text = "This is a\nnew line.";
const html = convertTextToHTML(text);

// Output: "This is a<br>new line."

In this example, the convertTextToHTML function takes a string of text as input and uses the replace method to replace new line characters (\n) with <br> HTML elements. The resulting HTML string is then returned.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply