Answer the question
In order to leave comments, you need to log in
What is the best way to write a regular expression so that part of the tag or its interior is not replaced?
In general, there is a task of highlighting words when searching. So far there is this code:
scrollTarget.each(function(){
//Обнуляем предыдущую подсветку
$(".searchResult").each(function() {
$(this).replaceWith(this.childNodes);
});
var expr = new RegExp(''+value+'', 'ig');
var highlightSearch = $(this).find('a');
//Получаем массив совпадений которые необходимо обернуть в подсветку
var found = highlightSearch.html().match(expr);
//Подсвечиваем
$.each(found,function(){
highlightSearch.html(highlightSearch.html().replace(this,'<i class="searchResult">'+this+'</i>'));
})
})
highlightSearch.html(highlightSearch.html().replace(this,'<i class="searchResult">'+this+'</i>'));
var highlightReg= new RegExp(загадочная регулярка);
highlightSearch.html(highlightSearch.html().replace(highlightReg,'<i class="searchResult">'+this+'</i>'));
<i class="searchResult">....</i>.
Answer the question
In order to leave comments, you need to log in
Don't make it difficult for yourself.
bartaz.github.io/sandbox.js/jquery.highlight.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question