JavaScript RegExp match text ignoring HTML

后端 未结 6 1949
时光取名叫无心
时光取名叫无心 2020-12-09 22:28

Is it possible to match \"the dog is really really fat\" in \"The dog is really really fat!\" and add \"

6条回答
  •  無奈伤痛
    2020-12-09 23:19

    Update:

    Here is a working fiddle that does what you want. However, you will need to update the htmlTagRegEx to handle matching on any HTML tag, as this just performs a simple match and will not handle all the cases.

    http://jsfiddle.net/briguy37/JyL4J/

    Also, below is the code. Basically, it takes out the html elements one by one, then does a replace in the text to add the highlight span around the matched selection, and then pushes back in the html elements one by one. It's ugly, but it's the easiest way I could think of to get it to work...

    function highlightInElement(elementId, text){
        var elementHtml = document.getElementById(elementId).innerHTML;
        var tags = [];
        var tagLocations= [];
        var htmlTagRegEx = /<{1}\/{0,1}\w+>{1}/;
    
        //Strip the tags from the elementHtml and keep track of them
        var htmlTag;
        while(htmlTag = elementHtml.match(htmlTagRegEx)){
            tagLocations[tagLocations.length] = elementHtml.search(htmlTagRegEx);
            tags[tags.length] = htmlTag;
            elementHtml = elementHtml.replace(htmlTag, '');
        }
    
        //Search for the text in the stripped html
        var textLocation = elementHtml.search(text);
        if(textLocation){
            //Add the highlight
            var highlightHTMLStart = '';
            var highlightHTMLEnd = '';
            elementHtml = elementHtml.replace(text, highlightHTMLStart + text + highlightHTMLEnd);
    
            //plug back in the HTML tags
            var textEndLocation = textLocation + text.length;
            for(i=tagLocations.length-1; i>=0; i--){
                var location = tagLocations[i];
                if(location > textEndLocation){
                    location += highlightHTMLStart.length + highlightHTMLEnd.length;
                } else if(location > textLocation){
                    location += highlightHTMLStart.length;
                }
                elementHtml = elementHtml.substring(0,location) + tags[i] + elementHtml.substring(location);
            }
        }
    
        //Update the innerHTML of the element
        document.getElementById(elementId).innerHTML = elementHtml;
    }
    

提交回复
热议问题