包含p标签的字符串中高亮搜索内容

this.searchTxt 是查询的内容, string 有可能包含 p 标签。当查询 p 字母的时候需要避免标签的 p

//高亮
  heightLight(string) {
   let reg = new RegExp(this.searchTxt, "gi");
    let reg1 = /[0-9a-z]/i;
    let arr = ["/", "

"
, ", "

", ">", "<", ", "]; if (reg1.test(this.searchTxt)) { if (!arr.includes(this.searchTxt)) { if (this.searchTxt == "p") { string = string.replace(/(<\/?p[^>]*>|p)/gi, function(txt) { return `${txt}`; }); } else { string = string.replaceAll(reg, function(txt) { return `${txt}`; }); } } } else { if (!arr.includes(this.searchTxt)) { string = string.replaceAll( this.searchTxt, `${this.searchTxt}` ); } } return string; }

你可能感兴趣的:(javascript,前端)