Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

循环后台返回的数据如下结构

 <div :key="num">
      <p class="sentence">
        {{ i.sentence }}
        <span>{{ i.source }}</span>
      </p>
      <p class="sentence">{{ i.senInterpret }}</p>
 </div>

其中i.sentence 是字符串"This is a pig" 渲染的页面上
This is a pig

有一个全局的变量 this.word = pig 的时候

要高亮 这个pig


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

//高亮

hightlight(str) {
  const search = this.word;
  var subStr = new RegExp(search); // 匹配关键字正则
  let replaceString = '<span class="highlights-text">' + search + "</span>"; // 高亮替换v-html值
  let result = str.replace(subStr, replaceString);
  return result;
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...