/**
* 关键词高亮方法
* searchStr 后台接口数据
* keyword 关键词
* color 高亮颜色
*/
const brightenKeywordFn = (searchStr, keyword,color) => {
if (keyword && keyword.length > 0) {
// let keywordArr = keyword.split('')
let keywordArr = [keyword]
searchStr = searchStr + ''
keywordArr.forEach((item) => {
if (searchStr.includes(item) && item !== '') {
searchStr = searchStr.replace(
new RegExp(item, 'g'),
`<span style="color:${color}">` + item + '</span>'
)
}
})
}
return searchStr && searchStr !== 'undefined' ? searchStr : '--'
}
const str = '落霞与孤鹜齐飞,秋水共长天一色'
const key ='秋水'
const color = 'pink'
brightenKeywordFn(str,key,color)
console