JSFiddle - React, Tailwind, and code Playground

by Axelvaisper

HTML

<h1>Ищем и выделяем слова: «dolor», «sit» и «elit»</h1>
<div class="content">
Lorem ipsum <a href="#">dolor</a> sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

CSS

html,body {
    height: 100%;
    margin: 0;
    padding: 0;
}
body {
    color: #333;
    font: 12px/1.2em Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 20px;
    background:#E6E6E6;
}
h1 {font:20px/1.2em arial}
.highlight { background:#0C3; color:#fff;}

JavaScript

$(function(){
    (function($){
    $.fn.liHighLight = function(params){
        var p = $.extend({
            words: '',
            class: 'highlight'
        }, params);
        return this.each(function(){
            var wrap = $(this);
            var wArr = $.trim(p.words).split(' ');
            htmlreplace($(this));
            function htmlreplace(element){
                if (!element) element = document.body;
                var wrap = $(element).contents().each(function () {
                    if (this.nodeType === 3) {
                        var result = $(this).text();
                        for(i = 0; i < wArr.length; i++){
                            result = result.replace(new RegExp(wArr[i],'gi'),'<span class="'+p.class+'">$&</span>');
                        }
                        $(this).after(result).remove();
                    } else {
                        htmlreplace(this);
                    };
                });
            };
        });
    };
})(jQuery);
    $('.content').liHighLight({
        words:'dolor sit elit',
        class: 'highlight'
    });
});