Created
November 5, 2019 11:22
-
-
Save TheDhejavu/2704fb3990c4a7056bcd49dda0f1eccf 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