Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache: How to convert carriage returns to <br> tags

It's late and my eyes are tired, so maybe I'm missing something obvious.

If I render with a Mustache template like this...

$(container).append(Mustache.render(template, { someText: someText}));

...and my template looks like this...

<div>{{someText}}</div>

...and someText is a long string that includes one or more carriage returns, how can I convert the carriage returns to <br/> tags?

At the moment, the carriage returns are left as-is, which means the markup looks nice, but the rendered text is all bunched up.

Thanks.

like image 584
Fijjit Avatar asked Dec 07 '25 08:12

Fijjit


1 Answers

To replace all carriage returns you can use:

someText = someText.replace(/\n/g,"<br />");
like image 183
Antonio Avatar answered Dec 08 '25 21:12

Antonio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!