After a user submits input, I am generating comments and displaying them using Mustache.js. When it comes to rendering the comments, I have found that replacing user-entered line breaks (\n
) with <br/>
allows them to display as HTML breaks.
To achieve this, I use the following code snippet:
myString.replace(/\n/g, '<br />');
I am aware that utilizing triple brackets in Mustache, like so - {{{myString}}}
, prevents HTML from being escaped.
However, what I really want is to escape all HTML entered by the user, except for allowing line breaks to be displayed as <br/>
. Is there an efficient way to accomplish this without having to replace line breaks after the content has been rendered? Your insights would be greatly appreciated!