I am trying to draw attention to the correct search for keywords. A couple of questions that I have.
- Case insensitivity works for the first word, but would like it to replace the original word of the word, and not the lower case word.
i.e. search trend, it replaces the trend with the trend, I know why, but I would like to find out how to replace the found word, not the search word
- The second word is not case-insensitive.
i.e. search trend micro does not match Micro trend.
Here is jsFiddle: http://jsfiddle.net/hh2zvjft/1/
if ($(".ProjectSearch").val().length > 0) { var searchedText = $(".ProjectSearch").val(); var wordList = searchedText.split(" "); $.each(wordList, function (i, word) { $(".ProjectTaskGrid:contains('" + word + "')").each(function (i, element) { var rgxp = new RegExp(word, "gi"); var repl = '<span class="search-found">' + word + '</span>'; element.innerHTML = element.innerHTML.replace(rgxp, repl); }); }); }
Could you help identify problems and suggest improvements? Thanks!
Some links used to get the code:
stack overflow
stack overflow
javascript jquery regex
Adamroof
source share