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

158
Visualizações
Styling a specific word in a data gotten from an api which is populated on a screen

After sending a to an API with a 'search value' that searches the database for the value and respond with data containing the value. I want to highlight the given word or search value in the response. The respond is populated to the UI

<center>
    {Object.keys(Disp).map((key, i) => (
      <center key={i} className="PublicationCard">
        <span> {key}</span>
        <a href={"https://www." + Disp[key]}> View Publication</a>
      </center>
    ))}
  </center>

The Key is a sentence but I want to bolden a word in the key For example, let say the key is "Discussions about Supervised Artificial Intelligence" and the search query was artificial intelligence, all I want to do is to bold the search query that is "artificial intelligence' in the UI

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe you can create a function to generate multiple span tags.

function GenerateSpan({ text, search }) {
    const reg = new RegExp(`(.*)(${search})(.*)`, "g")
    const array = [...text.matchAll(reg)]
    if (array.length > 0) {
        return array[0].slice(1).map((textPart, index) => {
            return <span key={index} className={textPart === search ? "highlight" : ""}>{textPart}</span>
        })
    } else {
        return <span>{text}</span>
    }
}

And use in your code :

<GenerateSpan text=key search="Supervised"/>

And then add style for class "highlight

.highlight{
font-weight: bold
}

So Finally :

<center>
    {Object.keys(Disp).map((key, i) => (
      <center key={i} className="PublicationCard">
        <GenerateSpan text=key  search="Supervised"/>
        <a href={"https://www." + Disp[key]}> View Publication</a>
      </center>
    ))}
  </center>
about 4 years ago · Juan Pablo Isaza Relatório

0

The example with RegExp works faster, but here is a simpler way.

function Highlighter({ highlight = '', children: text }) {
  const index = text.toLowerCase().indexOf(highlight.toLowerCase());

  if (index >= 0 && highlight.length) {
    const beforeHighlight = text.substring(0, index);
    const highlightedPart = text.substring(index, index + highlight.length);
    const afterHighlight = text.substring(index + highlight.length);

    return (
      <highlight-text>
        {beforeHighlight}
        <span>{highlightedPart}</span>
        {afterHighlight}
      </highlight-text>
    );
  }

  return <>{text}</>;
}

export default Highlighter;

And use it like:

<Highlighter highlight={'Recursion'}>
  {'Recursion a misunderstood topic'}
</Highlighter>
about 4 years ago · Juan Pablo Isaza Relatório
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