当我们要检测某一段字符串中是否包含链接的时候,根据一般的方法是检测是否包含某段字符串.下面是我总结的一个检测链接的方法
function to_link(txtContent)
{
var check_www='w{3}'+'[^\\s]*';
var check_http='(https|http|ftp|rtsp|mms)://'+'[^\\s]*';
var strRegex=check_www+'|'+check_http;
var httpReg=new RegExp(strRegex,'gi');
formatTxtContent = txtContent.replace(httpReg, function (httpText)
{
if(httpText.search('http')<0&&httpText.search('HTTP')<0)
{
return '<a href="' + 'http://' + httpText + '" target="_blank">' + httpText + '</a>';
}
else
{
return '<a href="'+ httpText + '" target="_blank">' + httpText + '</a>';
}
});
return formatTxtContent;
}