Replace part of string with tag in JSX

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

    Wrote a utility function for jsx.

    const wrapTags = (text: string, regex: RegExp, className?: string) => {
      const textArray = text.split(regex);
      return textArray.map(str => {
        if (regex.test(str)) {
          return {str};
        }
        return str;
      });
    };
    
    

提交回复
热议问题