简记,自定义指令,将文本中的链接转换成可点击a标签,正则匹配http:// 或者https:// 或者www 等
directives: {
// 使用局部注册指令的方式
openLink(el) {
// 获取内容
let textR = el.innerText;
// 判断内容是否为空
if (textR.length) {
// 匹配超链接正则
let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g;
textR = textR.replace(urlPattern, function (match) {
var href = match;
if (match.indexOf("http") == -1) {
href = "http://" + match; //** 加粗样式 **
}
return `<a target='_blank' href= ${href} > ${match} </a>`;
});
el.innerHTML = textR;
} else {
el.innerHTML = "";
}
},
}