keywordscolorful(keyword, title) {
//根据需求,对拿到的kryword进行处理
var keywords = keyword
.replace(/\+/g, "")
.replace("$kw:", "")
.replace(/\d+/g, "")
.trim()
.split(" ");
let re = "";
for (let i = 0; i < keywords.length; i++) {
if (i == keywords.length - 1) {
if (keywords[i].length > 0) {
re += keywords[i];
}
} else {
if (keywords[i].length > 0) {
re += keywords[i] + "|";
}
}
//利用RegExp动态生成正则表示式
let r = new RegExp(keywords[i], "ig");
//用title与其对比
if (r.test(title)) {
if (keywords[i].length > 0) {
title = title.replace(
r,
"<font color='red'>" + keywords[i] + "</font>"
);
}
this.goodsTitle = title; //得到标红的新标题
} else {
this.goodsTitle = title;
}
}
},
网友评论