简体   繁体   English

如何用HTML选择性地替换文本?

[英]How to selectively replace text with HTML?

I have an application where users can write comments containing HTML code, which is escaped before displaying: 我有一个应用程序,用户可以在其中编写包含HTML代码的注释,在显示之前将其转义:

<div class="card-body">
 <p class="card-text">
  &lt;h1&gt;HOLA&lt;/h1&gt; Cita:#2&lt;h1&gt;HOLA&lt;/h1&gt; &lt;h1&gt;HOLA&lt;/h1&gt; Cita:#6&lt;h1&gt;HOLA&lt;/h1&gt; &lt;h1&gt;HOLA&lt;/h1&gt; 
 </p>
</div>

But when a user write a specific word like "Cita:#1" I want to transform it with jQuery to a link, so later I can load an Ajax popup there with this code: 但是,当用户编写一个特定的单词,例如“ Cita:#1”时,我想用jQuery将其转换为链接,因此以后我可以使用以下代码在其中加载Ajax弹出窗口:

$('.card-text').each(function() {
    $(this).html($(this).text().replace(/Cita:#(\d+)/ig, '<a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="Some content">Cita:#$1</a>'));
});

My problem is that it does it well but also transform all possible HTML tags inside that comment too. 我的问题是,它做得很好,但同时也转换了该注释内的所有可能的HTML标签。

Is there a way to just ignore all tags that can be inside the comment and only replace the word "Cita:#1" with a link and made it works? 有没有一种方法可以忽略注释中的所有标记,而仅用链接替换“ Cita:#1”一词并使之起作用?

Actual: 实际:

在此处输入图片说明

Expected: 预期:

在此处输入图片说明

Since you control the server-side here, this would be much easier to do in PHP: 由于您在此处控制服务器端,因此在PHP中这样做会容易得多:

$string = '<h1>HOLA</h1> Cita:#2<h1>HOLA</h1> <h1>HOLA</h1> Cita:#6<h1>HOLA</h1> <h1>HOLA</h1>';
$string = htmlspecialchars($string);
$string = preg_replace(
    '/Cita:#(\\d+)/i',
    '<a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="Some content">Cita:#$1</a>',
    $string
);
echo $string;

Output: 输出:

&lt;h1&gt;HOLA&lt;/h1&gt; <a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="Some content">Cita:#2</a>&lt;h1&gt;HOLA&lt;/h1&gt; &lt;h1&gt;HOLA&lt;/h1&gt; <a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="Some content">Cita:#6</a>&lt;h1&gt;HOLA&lt;/h1&gt; &lt;h1&gt;HOLA&lt;/h1&gt;

What you need to do is separate the matching parts of the text and process them as HTML strings, but escape* the rest of the text. 您需要做的是将文本的匹配部分分开,并将它们作为HTML字符串进行处理,但是对其余文本进行转义。 Since regular expression matches provide the index of the match, this is easy enough to do. 由于正则表达式匹配项提供了匹配项的索引,因此这很容易做到。

 $('.card-text').each(function() { var before = ""; var after = $(this).text(); var link = ""; // look for each match in the string while (match = after.match(/\\b(Cita:#(\\d+))\\b/i)) { // isolate the portion before the match and escape it, this will become the output before += $("<div>").text(after.substring(0, match.index)).html(); // isolate the portion after the match, for use in the next loop after = after.substring(match.index + match[1].length); // build a link and append it to our eventual output before += match[1].replace(/Cita:#(\\d+)/i, '<a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="Some content">Cita:#$1</a>'); } // deal with the final bit of the string, which needs to be escaped this.innerHTML = before + $("<div>").text(after).html(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="card-body"> <p class="card-text"> &lt;span class="foo"&gt;Here is some text. &lt;b&gt;Cita:#1&lt;/b&gt; and &lt;b&gt;Cita:#2&lt;/b&gt;&lt;/span&gt; </p> </div> 


*We use jQuery to build an element, pass the string as the text content of that element, and finally get the HTML output: *我们使用jQuery来构建一个元素,将字符串作为该元素的文本内容传递,最后得到HTML输出:

htmlString = "<div>foo</div>";
console.log(
    $("<div>").text(htmlString).html()
);

Output: 输出:

&lt;div&gt;foo&lt;/div&gt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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