Replace part of string with tag in JSX

前端 未结 12 1261
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 09:39

I\'m trying to replace parts of a string with JSX tags, like so:

render: function() {
    result = this.props.text.replace(\":\",
12条回答
  •  再見小時候
    2020-12-16 10:15

    i think this is the most light perfect solution:

    render() {
        const searchValue = "an";
        const searchWordRegex = new RegExp(searchValue, "gi");
        const text =
          "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text";
        return (
          
    {text.split(searchWordRegex).length > 1 ? text.split(searchWordRegex).map((chunk, index) => { if (chunk !== "") { return index === 0 && ! new RegExp("^" + searchValue, "gi").test(text) ? ( chunk ) : ( {searchValue.charAt(0).toUpperCase() + searchValue.slice(1)} {chunk} ); } else { return null; } }) : text}
    ); }

    and here is a working example

提交回复
热议问题