2

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"
]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

3 Answers3

2

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);
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
2

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);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563