判断是否是 网址?
var URL_REG = '((http(s)|ftp)?://.)?(www\.)?[-a-zA-Z0-9@:%._\+~#/=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?(&|&)//=]*)' ;// 判断是否网址1
URL_REG = new RegExp(URL_REG, 'gi');
'输入的字符串'.replace(URL_REG , (item, index) => { });
var CURRENT_HOST = location.protocol + '//' + location.host;
var URL_REG = /https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g; // 判断是否网址2
str.replace(URL_REG, (t) => {
if (t.indexOf(CURRENT_HOST ) !== -1) {
return `<a href="${t}">${t}</a>`;
}
return `<a href="${t}" target="_blank">${t}</a>`;
});