Answer the question
In order to leave comments, you need to log in
How to highlight text in html?
How to highlight text between html tags? I have a finished article in the form of html, it is necessary to highlight the relevant words when searching.
If the text is not framed by html tags, then everything is fine. But how to implement selection in such text, for example:
<p><b>Хабр</b> помоги найти ответ на вопрос</p>
Here is what is now:
Vue.js
search= ""
let words = el.text.split(' ')
for (let key in words) {
if (words[key].includes(this.search)) {
words[key] = "<mark>" + words[key] + "</mark>";
}
}
el.text = n.join(" ");
Answer the question
In order to leave comments, you need to log in
methods: {
marked: (text, search) => search
? text.replace(RegExp(search.replace(/[\\^$|.*?+{}()[\]]/g, '\\$&'), 'gi'), '<mark>$&</mark>')
: text,
},
<div v-html="marked(text, search)"></div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question