JSFiddle - React, Tailwind, and code Playground

by Terry King

HTML

<textarea name="inp" id="inp" cols="45" rows="5"></textarea>
<button id="submit">Send</button>
<div id="commenttext"></div>

JavaScript

function nl2br(str, is_xhtml)
{
   var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
   return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

$('#inp').val("line'1'\nline\"2\"<button>button></button>\nline3&4");

$('#submit').click(function(){
    text=$('#inp').val();
    text=text.replace(/</g,"&lt;").replace(/>/g,"&gt;");
//    .replace(/\n/g,"<br />");
    
    text=nl2br(text);
    $('#commenttext').html(text);
    console.log(text);
});