替换 html 字符串中的某些“<”和“>”

Replacing certain "<" and ">" in html string

本文关键字:字符串 html 替换      更新时间:2023-09-26
<table class="sentinelresultitems" style="width: 800px; font-size: 0.5em; margin-top: 30px; margin-left: 50px; margin-right: 50px; display: table;">
    <tbody><tr>
        <td>
            <b>License ID</b>
        </td>
        <td style="text-align: right">
            <b>Product</b>
        </td>
     /tr>
    <tr>
        <td>New</td>
        <td class="quoteproductname" style="text-align: right">Product > name</td>
    </tr>
    <tr>
        <td>New</td><td class="quoteproductname" style="text-align: right">Another < product name
        </td>
    </tr>
    </tbody>
</table>

我试图用 c# 从上面的 html 字符串中生成一个 PDF。但它崩溃了,因为我无法在产品名称中分隔<>,从而导致无效的 html。即使我用&gt;等替换它们,它们仍然会被解析为>.

是否可以将<>替换为类似 GTLT 的东西,直到我将其放在服务器上?之后,我可以对此进行普通的文本替换。否则,我显然也会替换所有标签。

提前致谢

这就是你想要的?

var replacers = {
    '<': 'GT',
    '>': 'LT'
};
var html = $('.sentinelresultitems').html().replace(/[<>]/g, function(match) {
    return replacers[match];
});