Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
how to use react-highlight-words to highlight multiple keywords with different colors

I am developing a search function in my React project. In the beginning, I only need to input one keyword and search for it in the text, save the sentences with this keyword and display the result sentences separately. I am using "react-highlight-words" https://github.com/bvaughn/react-highlight-words to highlight this single keyword in all the search result sentences.

{searchResults.map((result, idx) => {
      
      return (
        <div key={`search-result-${idx}`}>
            <br />
            <Highlighter
              highlightClassName='YourHighlightClass'
              searchWords={[textForSearch]}
              autoEscape={true}
              textToHighlight={finalResults[idx]}
              key={idx}
            />
          </div>
        </div>
      );
    })}

above textForSearch is the keyword finalResults is the search result. I use a keyword to loop through the text and save the match sentences as elements to an array.

But now I need to input multiple keywords in one search and display some sentences or a paragraph containing these keywords and highlight different keywords with different colors.

how can I use this component to do it? and if some other way to do it please tell me. I have seen many topics and answers related to search and highlight but they all highlight different words with the same color.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda