小程序搜索文字高亮 可以用提供的基础富文本组件rich-text来渲染处理过的高亮数据,适用简单展示搜索列表
思路是例如正则匹配替换关键词
highlightText(text,key) {
const regex = new RegExp(key, 'gi');
return text.replace(regex, match => `<span style='color:red'>${match}</span>`);
},
利用工具函数 遍历处理需要高亮的数据
let tempArr = []
if (dataList && dataList.length) {
tempArr = dataList.map(item => {
return {
name: this.highlightText(需要处理的数据,高亮关键字)
}
})
}
this.setData({
showList: tempArr
})