【问题标题】:EJS not converting message to html formatEJS 没有将消息转换为 html 格式
【发布时间】:2021-01-11 21:18:09
【问题描述】:

我正在使用一个喊话框系统,我想将任何 URL 变成实际的可点击链接;

我目前拥有的是

<p class="da-msg-content" style="white-space: pre-wrap">
  <%=docs[i].msg.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, `<a href="$1"> $1 </a>`) %>
</p>

这会将https://example.com 之类的任何内容替换为实际链接,但实际上是这样

&lt;a href="https://example.com"&gt; https://example.com &lt;/a&gt; 而不是使其成为合法的 HTML

例子:

【问题讨论】:

    标签: node.js express ejs


    【解决方案1】:

    没关系,想通了&lt;%- ... %&gt; 而不是&lt;%= ... %&gt;

    区别:

    &lt;%- ... %&gt; 不转义。

    &lt;%= ... %&gt; 转义其中的任何内容。

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2011-11-16
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      相关资源
      最近更新 更多