针对html字符串进行关键字高亮匹配
highlightKeyword(keyword, htmlString) {
const str = keyword.trim().replace(/\s+/g, ',')
const arr = str.split(',')
const _arr = arr.map(val => {
return val.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
})
const regex = new RegExp(`(${_arr.join('|')})`, 'gi')
const newHtmlString = htmlString.replace(regex, match => {
this.totalCount += 1
return `<span id="keyword_highlight_id_${this.totalCount}" style="color: #865224; background: #FCE5C7;">${match}</span>`
})
return newHtmlString
},
本文来自博客园,作者:hong_li,转载请注明原文链接:https://www.cnblogs.com/hong1/p/19131310

浙公网安备 33010602011771号