直接将字符串当参数,调用该函数即可
function getHref(content) {
if (!content) {
return "";
}
let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g;
content = content.replace(urlPattern, function(match) {
var href = match;
if (match.indexOf("http") == -1) {
href = "http://" + match;**加粗样式**
}
return "<a target=\"_blank\" href=\"" + href + "\">" + match + "</a>";
});
return content;
}