JQuery代碼:
var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\%\!\&=\+\~\:\#\;\,]*)?)/ig;
var url = $('#mytest').html();
var thisurl = url.match(regexp);
console.info(thisurl);
html代碼:
<div id="mytest">
<img src="http://img.weibo.com/aa.jpg">
</div>
地址替換成鏈接:
var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\%\!\&=\+\~\:\#\;\,]*)?)/ig;
var url = $('#mytest').html();
document.write(replaceReg(regexp,url));
function replaceReg(reg,str){
return str.replace(reg,function(m){return '<a href="'+m+'">'+m+'</a>';})
}
<div id="mytest">
你是我的小丫小蘋果怎么愛你都不嫌多。。。。百度去吧http>//www.baidu.com錯誤那么多。。。。
</div>