Skip to content

Instantly share code, notes, and snippets.

@jketcham
Created January 27, 2016 23:26
Show Gist options
  • Select an option

  • Save jketcham/34b4906294b32382a92e to your computer and use it in GitHub Desktop.

Select an option

Save jketcham/34b4906294b32382a92e to your computer and use it in GitHub Desktop.

Revisions

  1. jketcham created this gist Jan 27, 2016.
    21 changes: 21 additions & 0 deletions matchSearchTerm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function matchSearchTerm(string, matchTerm) {
    if (!matchTerm) return string;

    var words = string.split(' ');
    var final = '';
    var parsedWords = [];

    words.forEach((word, index) => {
    if (word.toLowerCase().indexOf(matchTerm) > -1) {
    if (final) parsedWords.push(final);
    final = '';
    parsedWords.push(<span className="found_word" key={index}>{word} </span>);
    return;
    }

    final += word + ' ';
    });
    parsedWords.push(final);

    return parsedWords;
    }