您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

jquery分析文本里url或邮件地址为真实链接的方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   本文实例讲述了jquery分析文本里url或邮件地址为真实链接的方法。分享给大家供大家参考。具体如下:

  这段代码可以分析出文本里的所有超级链接,包含邮件、url、#链接等等,并分别输出为真实链接地址

  ?

1 2 3 4 5 6 7 8 9 10 11 $.fn.tweetify = function() { this.each(function() { $(this).html( $(this).html() .replace(/((ftp|http|https)://(w+:{0,1}w*@)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%@!-/]))?)/gi,'<a href="$1">$1</a>') .replace(/(^|s)#(w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>') .replace(/(^|s)@(w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>') ); }); return $(this); }

  用法:

   代码如下:

  $("p").tweetify();

  原始文本:

   代码如下:

  

@seanhood have you seen this http://icanhascheezburger.com/ #lol

 

  分析以后:

  ?

1 2 3 <p><a href="http://twitter.com/seanhood">@seanhood</a> have you seen this <a href="http://icanhascheezburger.com/">http://icanhascheezburger.com/</a> <a href="http://search.twitter.com/search?q=%23lol">#lol</a></p>

  希望本文所述对大家的jQuery程序设计有所帮助。