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

125
Visualizações
how can we bold the word before colon like aaaaa bb ccc need to be bold in react js

The regex is splitting string on word before colon, i am trying to bold the word before colon like aaaaa bb ccc need to be bold

str = "aaaaa: lorem ipsum bb: do lor sit amet ccc: no pro movet"
regex = / (?=\w+:)/g

splitString = str.split(regex)

console.log(splitString)

output of the above code is:

[
  "aaaaa: lorem ipsum",
  "bb: do lor sit amet",
  "ccc: no pro movet"
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Just modifying AnanthDev's answer to use replace which I think OP is trying to use.

const str = "aaaaa: lorem ipsum bb: do lor sit amet ccc: no pro movet";

const boldArr = str.replace(/(\b[^\s]+)(:)/g, `\n${'$1'.bold()}$2`).split('\n').filter(x => x).map(x => x.trim());
console.log(boldArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

As an alternative to splitting, you can match with 2 capture groups (denoted as m[1] and m[2] in the example code) and make the first group bold in the callback from Array.from

(\w+)(:.*?)(?=\s+\w+:|$)
  • (\w+) Capture 1+ word chars in group 1
  • (:.*?) Capture : and as least as possible chars in group 2
  • (?=\s+\w+:|$) Positive lookahead, assert 1+ word chars followed by : or the end of the string to the right

See a regex demo.

const regex = /(\w+)(:.*?)(?=\s+\w+:|$)/g
str = "aaaaa: lorem ipsum bb: do lor sit amet ccc: no pro movet"
str = Array.from(str.matchAll(regex), m => `<b>${m[1]}</b>${m[2]}`);
console.log(str);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use

var Component = React.createClass({
    render: function() {
        var text = this.props.text.split(/\s+(?=\w+:)/).map((address, index) =>
            <p key={index}>{ address.split(/(?=:)/).map((x,i)=> i===0 ? <b key={i}>{x}</b> : x) } </p> );
        
        return <div className="text">{text}</div>;
    }
});

var text = 'aaaaa: lorem ipsum bb: do lor sit amet ccc: no pro movet';
ReactDOM.render(
    <Component text={text} />,
    document.getElementById('container')
);
p {
    margin: 0;
    padding: 0;
}
<script src="https://unpkg.com/react@0.14.0/dist/react-with-addons.js"></script>
<script src="https://unpkg.com/react-dom@0.14.0/dist/react-dom.js"></script>

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

Details:

  • .split(/\s+(?=\w+:)/) - splits the str string with one or more whitespace chars that are directly followed with 1+ word chars and then a : char
  • .map((address, index) => <p key={index}>{ address.split(/(?=:)/).map((x,i)=> i===0 ? <b key={i}>{x}</b> : x) } </p> ) - takes each split and assigns its value to address and index to index, wraps the item with <p key=index> / </p> and modifies the item the way explained below...
  • address.split(/(?=:)/).map((x,i)=> i===0 ? <b key={i}>{x}</b> : x) - each address string is split at the location right before a : char, and all even occurrences are wrapped with <b> tag, odd ones are kept as is.

Note that the key attribute is necessary for React to be able to handle minimal DOM changes, see Understanding unique keys for array children in React.js.

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