keyLight(key, color, content) {
color = color || "red";
sText = "";
sText = content;
var sKey = "<span style='color: " + color + ";'>" + key + "</span>";
var num = -1,
rStr = new RegExp(key, "gi"),
rHtml = new RegExp("<.*?>", "ig"), //匹配html元素
aHtml = sText.match(rHtml); //存放html元素的数组
sText = sText.replace(rHtml, "{~}"); //替换html标签
sText = sText.replace(rStr, sKey); //替换key
sText = sText.replace(/{~}/g, function() {
//恢复html标签
num++;
return aHtml[num];
});
this.contentDetails.content = sText;
},
通过搜索关键字去匹配文章里的内容并将关键字高亮显示且不匹配标签
最新推荐文章于 2021-06-04 07:37:42 发布