Replace part of string with tag in JSX

前端 未结 12 1246
被撕碎了的回忆
被撕碎了的回忆 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:20

    The accepted answer is two years old. For this problem issue #3368 was created and based on the solution provided by a Facebook employee working on React, react-string-replace was created.

    Using react-string-replace, here is how you can solve your problem

    const reactStringReplace = require('react-string-replace');
    
    const HighlightNumbers = React.createClass({
      render() {
        const content = 'Hey my number is 555:555:5555.';
        return (
          
            {reactStringReplace(content, ':', (match, i) => (
              
    ))}
    ); }, });

提交回复
热议问题