-3

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.

pedrum golriz
  • 513
  • 8
  • 27
  • 2
    What exactly are you trying to do? If you're putting text in a HTML element and want to display that text including line-breaks, just set `white-space: pre-wrap;` in element's styles. – hon2a Nov 25 '14 at 16:09
  • @hon2a oh wow, don't know how that didn't occur to me. Thank you. – pedrum golriz Nov 25 '14 at 16:12

1 Answers1

-1

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.

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67