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