The functionality of my search engine is quite similar to how Google search works. When you search for a word, it gets highlighted in the returned results.
https://i.sstatic.net/Y1vc0.png
In my search engine, the searched word is enclosed in a span like this:
<span class="searchResults-term">Lorem</span>
The input field for entering search queries is very basic:
<input type="text">
The search result descriptions are displayed within this div:
<div class="searchResults-result">
<h3 class="searchResults-title">Lorem Ipsum
</h3>
</div>
I am trying to figure out how to highlight the searched word (e.g., Lorem or Ipsum) in the description based on what the user has typed in the input field. It should not be case-sensitive.
I have been struggling with this issue for hours and have spent an entire day researching solutions. Any help would be greatly appreciated. Thank you!
I have started writing jQuery code for this purpose, but I suspect that my setup may not be correct.
var oldSRTerm = $(".span.searchResults-term").val();
newSRTerm = '<span class="add-pink-bg">' + $(".span.searchResults-term").val() + '</span>',
newSRDesc = $('.searchResults-description').val().replace(RegExp(oldSRTerm,"gi"),newSRTerm);
$('.searchResults-description').html(newSRDesc);