實(shí)現(xiàn)方案:用正則全局替換
// 避免搜索特殊字符狈邑,如“\”時(shí)報(bào)錯(cuò)袄简,對(duì)value進(jìn)行轉(zhuǎn)義
const regName = new RegExp(`\\${value}`, 'g');
const itemLists = this.indexTreeDataLists.filter(o => o.name.match(regName));
if (itemLists.length) {
itemLists.forEach(item => {
const result = [];
this.helpGetSearchName(item.parent, result);
let resultStr = `${result.join('/')}${item.parent ? '/' : ''}${item.name}`;
// 替換搜索結(jié)果
resultStr = resultStr.replace(regName, `<span style="color:#0090FF;">${value}</span>`);
}
// 幫助 - 搜索 - 拼接搜索結(jié)果
helpGetSearchName(data, result = []) {
if (!data) return;
if (data.parent) {
this.helpGetSearchName(data.parent, result);
}
result.push(data.name);
},