高亮匹配关键词样式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
    },
相关推荐
广州灵眸科技有限公司1 小时前
瑞芯微(EASY EAI)RV1126B 核心板供电电路
linux·运维·服务器·单片机·嵌入式硬件·电脑
keyipatience1 小时前
18.Linux进程退出和进程等待机制详解
linux·运维·服务器
wanhengidc3 小时前
私有云的作用都有哪些?
运维·服务器·网络·游戏·智能手机
Java面试题总结3 小时前
java高频面试题(2026最新)
java·开发语言·jvm·数据库·spring·缓存
花阴偷移3 小时前
Ubuntu 22.04版本下配置静态IP
linux·运维·服务器·tcp/ip·ubuntu
苦逼的猿宝3 小时前
学生心理咨询评估系统
java·毕业设计·springboot·计算机毕业设计
隔窗听雨眠3 小时前
doctype、charset、meta如何控制整个渲染流水线
java·服务器·前端
魔法阵维护师4 小时前
从零开发游戏需要学习的c#模块,第十六章(安装 MonoGame 并创建第一个窗口)
学习·游戏·c#·monogame
tedcloud1234 小时前
TradingAgents部署教程:打造AI量化分析工作流
服务器·前端·人工智能·系统架构·edge