匹配url 添加为a标签

var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
if(str.indexOf('<a href=')==-1){  // 过滤下a标签
str= str.replace(reg, "<a href='$1$2'>$1$2</a>");
}
2、文本换行处理
breakStr(str) {
var regBr = /(\r\n)|(\n)|(\↵)/g;
str = str.toString().replace(regBr, "<br>");
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g; 匹配Url
str = str.replace(reg, "<a href='$1$2' target='_blank'>$1$2</a>");
return str;
},

猜你喜欢

转载自www.cnblogs.com/xk-one/p/10078123.html