0
点赞
收藏
分享

微信扫一扫

vue关键字高亮有正则关键字报错

佛贝鲁先生 2022-02-17 阅读 170
    // str是整段文字,keyWord是需高亮文字
    highlightText (str, keyWord) {
      const key = '(' + keyWord.replace(/([\+\.\*\|\?\-\(\[\^\$])/g, '\\$1').replace(/\s+/g, '|') + ')'
      const pattern = new RegExp(key, 'g')
      const str2 = str.replace(pattern, `<span style="color: #f00;">${keyWord}</span>`)
      return str2
    },
举报

相关推荐

0 条评论