jquery正則匹配URL地址


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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM