高亮匹配关键词样式highLightMatchString、replaceHTMLChar

javascript 复制代码
    replaceHTMLChar: s => s.toString().replace(/</g, `&lt;`).replace(/>/g, `&gt;`),

    // 高亮匹配关键词样式----------------------------------------
    highLightMatchString(originStr, matchStr, customClass = ``) {
        matchStr && (matchStr = matchStr.replace(/[.*+?^${}()|[\]\\]/g, ``));//过滤掉字符串里面的出现的正则表达式字符
        if (matchStr === ``) return originStr;
        customClass === true && (customClass = `sg-search-word-highlight`);
        let newRepStr = (match = matchStr) => customClass ? `<span class=${customClass}>${this.replaceHTMLChar(match)}</span>` : `<b style='color:red;font-weight:bold;'>${this.replaceHTMLChar(match)}</b>`;
        // return this.replaceHTMLChar(originStr).replace(new RegExp(this.replaceHTMLChar(matchStr), `gi`), newRepStr(matchStr));
        return this.replaceHTMLChar(originStr).replace(new RegExp(this.replaceHTMLChar(matchStr), `gi`), (match, index) => newRepStr(match));// 返回原始匹配项,如果不希望替换,可以返回match
    },
相关推荐
学长毕业设计3 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
Brilliantwxx3 小时前
【Linux】 进程(9)程序与进程地址空间(基础+进阶+面试题)
linux·运维·服务器·开发语言·c++
东小西4 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西4 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午4 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
不怕犯错,就怕不做4 小时前
git prune 自动删除本地记录中那些远程已经不存在的分支引用
linux·服务器·git
2601_962065254 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_4 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
geovindu5 小时前
CSharp: 万年历
开发语言·后端·c#·.net
weixin_416667965 小时前
【无标题】
运维·服务器·网络