将文本中的url正则判断出来,并改为超链接形式
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/>")
}}
/>
);
}