Skip to content

Instantly share code, notes, and snippets.

@MartinMuzatko
Created June 30, 2016 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinMuzatko/93222bd97a328d8c9f2f23ffaf129823 to your computer and use it in GitHub Desktop.
Save MartinMuzatko/93222bd97a328d8c9f2f23ffaf129823 to your computer and use it in GitHub Desktop.
Finding multiple occurences of string and highlight them
(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