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

230
Visualizações
Underline specific words of a string in React

So I've got a string being sent to a class that is split by \n for format. I was wondering if it was possible to underline specific words of the string so it looks better?

Currently this is what is being passed

"Name : Jackson 
\nLabel: Movie
\nDirector: 
\nGenre: Action
\nActors: Will, Jack, Carrie
\nMetascore: 90/100
\nRevenue: 87.5M
\nRuntime: 100 min
\nRating: 9/10"

And I would like for the word after the \n until the ":" to be underlined but all I've seen are making an entire string underlined.

Heres how I'm currently parsing the string.

 const newText = text.split('\n').map(str => <p>{str}</p>);

My guess would be to have str split again by : inside the .map and using <Text style={styles.bold}> </Text> to set it?

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

0

You can split each line of your text by :, like below:

const string = `
Name: Jackson 
Label: Movie
Director: 
Genre: Action
Actors: Will, Jack, Carrie
Metascore: 90/100
Revenue: 87.5M
Runtime: 100 min
Rating: 9/10
`;

function MyList() {
  const strings = string.split('\n').map((str) => {
    const [label, value] = str.split(':');
    return (
      <div>
        <u>{label}</u>:<p>{value}</p>
      </div>
    );
  });

  return strings;
}

<u> stands for underline in html.

You can also add a className attribute to the tag surrounding the label part and use text-decoration: underline;

about 4 years ago · Juan Pablo Isaza Relatório

0

Try text.split(/[\n:]/).
After that - put each uneven element into a tag.
Of course it will work only if there are no other colons or new lines in text.

const str = "Name : Jackson\nLabel: Movie\nDirector: \nGenre: Action\nActors: Will, Jack, Carrie\nMetascore: 90/100\nRevenue: 87.5M\nRuntime: 100 min\nRating: 9/10"

console.log(str.split(/\s?[\n:]\s?/).map((s, i) => i % 2 === 0 ? ('<u>' + s + '</u>:') : (' ' + s + '\n')).join(''))

about 4 years ago · Juan Pablo Isaza Relatório

0

Split the string inside the map function, wrap it in span tags and use text-decoration: underline; for css.

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