使用html实体的Javascript replace()不起作用



我得到了以下脚本,它成功地替换了<和>,代码如下所示。这里的想法是,如果用户想在博客上用粗体显示"粗体我",他们会在文本框中输入。

$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&lt;', '<span class="bold">'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&gt;', '</span>'));
});

其他html实体也会出现问题。我只举我的例子。我想用一个段落标记替换[html实体,但这个脚本中的所有行都不起作用

$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&#x0005B;', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&lbrack;', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&lsqb;', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('&lbrack;', '<p>'));
});

对此有任何想法都将不胜感激!谢谢

字符'['不是字符实体,因此不进行编码。直接传递给replace:

string.replace('[' , '<p>')

最新更新