Home >Web Front-end >JS Tutorial >jquery method to analyze the url or email address in the text as a real link_jquery

jquery method to analyze the url or email address in the text as a real link_jquery

WBOY
WBOYOriginal
2016-05-16 15:53:461242browse

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

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

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

用法:

复制代码 代码如下:
$("p").tweetify();

原始文本:

复制代码 代码如下:
e388a4556c0f65e1904146cc1a846bee@seanhood have you seen this http://icanhascheezburger.com/ #lol94b3e26ee717c64999d7867364b1b4a3

分析以后:

<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&#63;q=%23lol">#lol</a></p>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn