Last active
December 10, 2020 10:45
-
-
Save TheDhejavu/5e14c1c7751358620bba38eba851f7db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let link_string = string; | |
let searchList = search.split(" "); | |
const highlightMatchesInString = (string, search)=>{ | |
var completedString = completedString || string; | |
search.forEach(function(item) { | |
var reg = "(" + item + ")(?![^<]*>|[^<>]*</)"; | |
var regex = new RegExp(reg, "i"); | |
//Highlight..... | |
var matchStartPosition = string.match(regex).index; | |
var matchEndPosition = matchStartPosition + item.length; | |
var originalTextFoundByRegex = string.substring(matchStartPosition, matchEndPosition); | |
completedString = completedString.replace(regex, `<strong>${originalTextFoundByRegex}</strong>`); | |
}); | |
return completedString; | |
} | |
string = highlightMatchesInString(string, searchList); | |
return `<li> | |
<a href="/search?q=${link_string.replace(/ /g,"+")}"> | |
<span>${string}</span> | |
</a> | |
<i class="ion-android-search"></i> | |
</li>` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment