Skip to content

Instantly share code, notes, and snippets.

@broslav broslav/.js
Created Sep 14, 2018

What would you like to do?
Function to highlight user input inside autosuggest component
highlightWords = (string, highlight) => {
let counter = 0;
let str = string;
let index = string.toLowerCase().indexOf(highlight.toLowerCase());
let result = [];
while(index > -1) {
result.push(<span key={counter++}>{str.slice(0, index)}</span>);
result.push(<mark key={counter++}>{str.slice(index, index + highlight.length)}</mark>);
str = str.slice(index + highlight.length);
index = str.toLowerCase().indexOf(highlight.toLowerCase());
result.push(<span key={counter++}>{str}</span>);
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.