wxml
<block wx:for="{{item.title}}" wx:key="*this" wx:for-item="items">
<text wx:if="{{items.key == true}}" style="color:red;">{{items.str}}</text>
<text wx:else>{{items.str}}</text>
</block>
js
let that = this
let data = '啊啊啊啊啊哈啊啊啊哈哈啊啊哈哈额'
let value = '哈哈';
// 调用处理的方法
data = that.hilight_str(key, data);
console.log(data)
// 根据关键字切割字符串
hilight_str: function (key, strs) {
let idx = strs.indexOf(key), t = [];
if(idx > -1) {
if(idx == 0) {
t = this.hilight_str(key, strs.substr(key.length))
t.unshift({ key: true, str: key })
return t;
}
if(idx > 0) {
t = this.hilight_str(key, strs.substr(idx));
t.unshift({ key: false, str: strs.substring(0, idx) });
return t
}
}
return [{ key: false, str: strs }];
},