对小程序的模糊查询操作的结果中与键入的关键字匹配的结果中使用rich-text将关键字的颜色改变
JS部分
subject.forEach(item => {
const base = item.schoolName + '-' + item.category
const em = `<span style="color:#2E86FF">${search}</span>`
const exp = new RegExp(search, 'g')
const schoolname = `<p style="margin-top:12px">${base.replace(exp, em)}</p>`
item.resultsubject = schoolname
})
this.setData({
subjectList: subject
})
wxml部分
<rich-text wx:for="{{subjectList}}" wx:key="index" nodes="{{item.resultsubject}}" class='richitem'></rich-text>