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

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

vue中高亮p标签中的某一个字符串

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

 <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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

//高亮

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;
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...