D
D
Deodatuss2015-09-19 16:30:06
JavaScript
Deodatuss, 2015-09-19 16:30:06

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>'));
                })
            })

So, at the moment it highlights the norms until a symbol from the wrapper comes across, and then the sad thing happens. So that's actually the question: How best to write a regular expression so that the string
highlightSearch.html(highlightSearch.html().replace(this,'<i class="searchResult">'+this+'</i>'));

rewrite to
var highlightReg= new RegExp(загадочная регулярка);
  highlightSearch.html(highlightSearch.html().replace(highlightReg,'<i class="searchResult">'+this+'</i>'));

and it didn’t touch when replacing a design like <i class="searchResult">....</i>.
I feel the answer is somewhere on the surface, but something doesn’t boil my head. Point out where to look. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2015-09-19
@miraage

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 question

Ask a Question

731 491 924 answers to any question