前端高亮搜索内容
方法:
// 处理搜索后值加粗样式
const highlightSearch = (text?: string) => {
if (text) {
const regex = new RegExp(`(${searchText})`);
return text?.replace(regex, (match) => `<span style="font-weight: 600">${match}</span>`);
} else {
return '--';
}
};
使用:
<span dangerouslySetInnerHTML={{ __html: highlightSearch(item[attrId]) }}></span>