多个关键词加红

多个关键词加红

lightWord(article, oldWord){
      for (const key of oldWord){
        let regExp = new RegExp(key, 'g');//正则表达式方法,完全匹配对应的关键字,且声明全局
        article = article.replace(regExp, `<span style="color:#f00">${key}</span>`);
      }
      return article;
    }
   
 //第一个参数是要加红的文字部分,第二个参数是关键词
this.lightWord(that.list.content,that.keywords)

 //在哪显示
 $('.main').html(that.lightWord(that.list.content,that.keywords))
发布了14 篇原创文章 · 获赞 2 · 访问量 3376

猜你喜欢

转载自blog.csdn.net/qq_43459332/article/details/90695774