function replaceKey(target, key) {
    var content = target.html();
    var newContent = content.replace(/key/g, "<span style='color:#0c72cb;font-size:16px;'>$&</span>");
    target.html(newContent);
}

如上这种写法肯定是不行的, /key/ 会被看成一个字符串,去匹配key

改成如下这种方式

function replaceKey(target, key) {
    var content = target.html();
    var re = new RegExp(key, 'g') 
    var newContent = content.replace(re, "<span style='color:#0c72cb;font-size:16px;'>$&</span>");
    target.html(newContent);
}

通过new 的方式 生成正则对象

相关文章:

  • 2021-12-21
  • 2022-12-23
  • 2021-12-26
  • 2021-06-21
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2021-05-26
  • 2022-12-23
  • 2021-08-27
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
相关资源
相似解决方案