【问题标题】:Regex filter returning object Object instead of element正则表达式过滤器返回对象对象而不是元素
【发布时间】:2021-02-15 18:45:33
【问题描述】:

我正在使用 React 和外部 API

我从一个外部 api 接收数据,其中包含 url, 即

sampleText = Ethereum’s original token distribution event, managed by the [Ethereum Foundation](https://messari.io/asset/ethereum)

我想将网址转换为链接:

 const turnIntoLink =(text)=>{
    const urlFilter = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
    return text.replace(urlFilter, (url)=>{
      return '<a href="' + url + '">' + url + "</a>";
    })
  }
turnIntoLink(sampleText)

当我使用上面的代码时,它会正确读取网址但返回

... managed by the [Ethereum Foundation](<a href="https://messari.io/asset/ethereum">https://messari.io/asset/ethereum</a>) 

当我将 turnIntoLink 更改为此

 const turnIntoLink =(text)=>{
    const urlFilter = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
    return text.replace(urlFilter, (url)=>{
      return <a href={url}>${url}</a>;
    })
  }

返回

... managed by the [Ethereum Foundation]([object Object])

更新的 CoinDetail.jsx 反映了反向引用的使用

import React from "react";

const CoinDetail = (props) => {
  const { profile } = props.details;
  const turnIntoLink = (text) => {
    const urlFilter = /\[([^\][]*)]\(((?:https?|ftps?|file):\/\/[^()]*)\)/gi;
    return text.replace(urlFilter, '<a href="$2">$1</a>');
  };
  const display = () => {
    return (
      <div>
        <div>
          Launch Details:
          {turnIntoLink(profile.economics.launch.general.launch_details)}
        </div>
      </div>
    );
  };
  return <section>{profile ? display() : "Loading"}</section>;
};

export default CoinDetail;

我怎样才能让它返回一个实际的 a 元素?

更新:

我能够通过在下面的返回中执行此操作来使其工作

   <span
            dangerouslySetInnerHTML={{
              __html: turnIntoLink(
                profile.economics.launch.general.launch_details
              ),
            }}
          ></span>

它有效,但我觉得这有点 hacky,并且有更好的方法来做到这一点。有吗?

【问题讨论】:

  • 当您需要to replace with the whole match 时,您不需要使用回调作为替换参数。有一个$&amp; 反向引用。
  • 我知道的唯一方法是拆分和连接字符串,同时在需要时将部分转换为正确的 JSX。不幸的是,我找不到这种方法的参考。
  • github.com/iansinnott/react-string-replace 怎么样?这将使您能够编写return ( &lt;div&gt; {reactStringReplace(content, /\[([^\][]*)]\(((?:https?|ftps?|file):\/\/[^()]*)\)/gi, (match, x, y) =&gt; ( &lt;a href={y}&gt;{x}&lt;/a&gt; ))} &lt;/div&gt;

标签: javascript reactjs regex


【解决方案1】:

React 中,您可以将以下代码与React String Replace 一起使用:

return (
 <div>
   {reactStringReplace(content, /\[([^\][]*)]\(((?:https?|ftps?|file):\/\/[^()]*)\)/gi, (match, x, y) => (  <a href={y}>{x}</a>  ))}
 </div>

如果你使用纯 JavaScript,你可以使用

const turnIntoLink = (text) => {
    const urlFilter = /\[([^\][]*)]\(((?:https?|ftps?|file):\/\/[^()]*)\)/gi;
    return text.replace(urlFilter, '<a href="$2">$1</a>');
}

请参阅regex demo详情

  • \[ - 一个 [ 字符
  • ([^\][]*) - 第 1 组:除 ][ 之外的任何零个或多个字符
  • ]\( - ]( 字符串
  • ((?:https?|ftps?|file):\/\/[^()]*) - 第 2 组:httphttpsftpftpsfile,然后是 :// 子字符串,然后是除 () 之外的任何零个或多个字符
  • \) - 一个 ) 字符。

查看 JavaScript 演示:

const turnIntoLink = (text) => {
    const urlFilter = /\[([^\][]*)]\(((?:https?|ftps?|file):\/\/[^()]*)\)/gi;
    return text.replace(urlFilter, '<a href="$2">$1</a>');
}

console.log( turnIntoLink("Ethereum’s original token distribution event, managed by the [Ethereum Foundation](https://messari.io/asset/ethereum)") );

【讨论】:

  • 我试过你的代码,它仍然返回 (messari.io/asset/ethereum">https://messari.io/asset/…)
  • @JoeCillo 我的代码返回Ethereum’s original token distribution event, managed by the &lt;a href="https://messari.io/asset/ethereum"&gt;Ethereum Foundation&lt;/a&gt;,请运行Run code sn-p
  • 是的,很抱歉它确实返回了那个,我复制了错误的文本,但它仍然作为文本而不是链接返回
  • @JoeCillo 你用 React 编码吗?如果有请更新问题,还请添加相关代码,如果可能,请添加可重现的代码 sn-p。
  • 是的,我正在使用 react,我更新了帖子以反映这一点并包含代码
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 2022-10-02
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多