Search Highlight in HTML text/element

Date: 2018-11-29
const getSearchHtml = (searchValue, item) => {
        let name = item.html || item.name;
        if (!searchValue) {
            return name;
        }
        let reg = new RegExp(escapeRegExp(searchValue), 'gi');
        var elementToSearch = document.createElement('div');
        elementToSearch.innerHTML = name.trim();
        Array.from(elementToSearch.children).forEach(element => {
            let text = element.textContent;
            text = String(text).replace(reg, (str) => '<span class="search-highlight">' + str + '</span>');
            element.innerHTML = text;
        });
        return elementToSearch.innerHTML;
    };

 

16620cookie-checkSearch Highlight in HTML text/element