将文本中的url正则判断出来,并改为超链接形式
![](https://i-blog.csdnimg.cn/blog_migrate/0b287a1434cdaec118ee9cdd3f4370c4.png)
const renderFormatUrl = (content: string) =>{
if (!content) {
return null;
}
let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g; // eslint-disable-line no-useless-escape
let preContent = content.replace(urlPattern, function(match: string) {
let href = match;
if (match.indexOf("http") === -1) {
href = "http://" + match;
}
return '<a target="_blank" href="' + href + '">' + match + "</a>";
});
return (
<div
style={{
display: "inline-block",
}}
dangerouslySetInnerHTML={{
__html: preContent
.toString()
.split("\n")
.join("<br/>")
}}
/>
);
}