Underscore replace new line characters with line break

1.8k Views Asked by At

I have a micro-template bringing in strings from a rest call, but the strings are not formatted in the way they were originally entered.

The following works:

<%= message.replace(new RegExp('\d', 'g'), '<br /><br />') %>

However, this allows scripts to be entered and will execute when the template is shown. I also tried this:

<%- message.replace(new RegExp('\d', 'g'), '<br /><br />') %>

But this just prints <br /> text in the html. Basically I need a combination of the two, allowing the template to create the new line without letting scripts entered from the rest call through.

1

There are 1 best solutions below

0
On

Since you are instantiating a new RegExp object from a string pattern, you have to escape \d:

new RegExp('\\d', 'g')

Doing new RegExp('\d', 'g') is the same as /d/g.