0

I have the following function being used to highlight a word when the user does a search. The search is returning all the correct information but the word is not always highlighted. It seems any word that is surrounded by a special character, such as a parenthesis or backslash, will not be highlighted. I tried performing a split on anything other a letter which seemed to fix the issue but the special character was removed from the search results. I am also wanting the results to return the special character when searched. Below is my code:

ighlight: function (value) {
        try {
            if (value) {
                var words = value.split(' ');
                //var words = value.split(/[^A-Za-z]/);
                var len = words.length;
                var searchTerm = RRS.Search.Query.Value.trim().toLowerCase();
                var searchTermLen = searchTerm.length;

                for (var i = 0; i < len; i++) {
                    //SKIP ANY WORDS THAT ARE NOT AT LEAST OF THE SAME LENGTH
                    if (words[i].length >= searchTermLen) {
                        var word = words[i].toLowerCase();
                        var isMatch = true;
                        var term = "";
                        var startIndex = 0;
                        var startTerm = "";
                        var k = 0;

                        if (word.indexOf('-') > -1) {
                            while (word[k] != '-') {
                                startTerm += word[k];
                                k++;
                            }
                            startTerm += word[k];
                            k++;
                        }
                        for (var j = 0; j < searchTermLen; j++) {
                            if (word[k] != searchTerm[j]) {
                                isMatch = false;
                                break;
                            } else {
                                term += words[i][k];
                            }
                            k++;
                        }

                        if (isMatch) {
                            var endTerm = "";
                            for (var j = k; j < word.length; j++) {
                                endTerm += words[i][j];
                            }
                            words[i] = startTerm + "<span class=\"rrsHighlight\">" + term + "</span>" + endTerm;
                        }
                    }
                }
                return words.join(' ');
            } else {
                return value;
            }
        } catch (e) {
            edmscPage.HideWaitScreenDialog();
            alert("Report->Highlight\n" + e.message);
        }
    },
Schmit
  • 41
  • 5
  • 12
  • 2
    Does this answer your question? [How to highlight text using javascript](https://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript) – imvain2 May 03 '23 at 19:12
  • @imvain2 Thank you for the response, I don't really see anything on that thread for the issue I am facing. I am looking for all instances of the word to be highlighted. The only issue I'm having is if the word is surrounded by a special character, it wont get highlighted. For example if I search for Delete, and there is a string in the results that are Destroy/Delete, it won't highlight Delete, but would highlight Destroy if I searched that. – Schmit May 03 '23 at 19:56

0 Answers0