简体   繁体   中英

Underscore replace new line characters with line break

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM