Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!