高亮匹配关键词样式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
    },
相关推荐
apocelipes7 小时前
golang unique包和字符串内部化
java·python·性能优化·golang
Full Stack Developme7 小时前
java.text 包详解
java·开发语言·python
fruge8 小时前
Ubuntu服务器已下载Nginx安装包的安装指南
服务器·nginx·ubuntu
刘梦凡呀8 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
承渊政道8 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio
best_virtuoso8 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian20148 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
future_studio8 小时前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
孤独得猿9 小时前
聊天室项目开发——etcd的安装和使用
linux·服务器·c++·etcd
楚韵天工9 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物