Created
June 30, 2016 11:47
-
-
Save MartinMuzatko/93222bd97a328d8c9f2f23ffaf129823 to your computer and use it in GitHub Desktop.
Finding multiple occurences of string and highlight them
This file contains 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
(find, content) => { | |
// EXAMPLE | |
// find = 'route found' | |
// content = 'we found, that routes are commonly misinterpreted' | |
// return: 'we <b>found</b>, that <b>route</b>s are commonly misinterpreted' | |
var searchQueries = find.toLowerCase().split(' ') | |
// highlight all parts of strings that were found | |
for (var searchQuery in searchQueries) { | |
searchQuery = searchQueries[searchQuery] | |
if (!!~content.toLowerCase().indexOf(searchQuery)) { | |
content = content.replace( | |
// replace occurences only once | |
new RegExp(`(${searchQuery})(?!\<\/b\>)`, 'i'), | |
'<b>$1</b>' | |
) | |
} | |
} | |
return content | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment