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

May 16, 2016 pm 03:53 PM
jquery url text mail Link

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

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

$.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);
}
Copy after login

用法:

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

原始文本:

复制代码 代码如下:
<p>@seanhood have you seen this http://icanhascheezburger.com/ #lol</p>

分析以后:

&lt;p&gt;&lt;a href="http://twitter.com/seanhood"&gt;@seanhood&lt;/a&gt; have you seen this
&lt;a href="http://icanhascheezburger.com/"&gt;http://icanhascheezburger.com/&lt;/a&gt;
&lt;a href="http://search.twitter.com/search&#63;q=%23lol"&gt;#lol&lt;/a&gt;&lt;/p&gt;

Copy after login

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

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Outlook emails lost from control panel in Windows 11 Outlook emails lost from control panel in Windows 11 Feb 29, 2024 pm 03:16 PM

Outlook emails lost from control panel in Windows 11

How to download links starting with 115://? Download method introduction How to download links starting with 115://? Download method introduction Mar 14, 2024 am 11:58 AM

How to download links starting with 115://? Download method introduction

How to get the WeChat video account link? How to add product links to WeChat video account? How to get the WeChat video account link? How to add product links to WeChat video account? Mar 22, 2024 pm 09:36 PM

How to get the WeChat video account link? How to add product links to WeChat video account?

What are the requirements for a video link? How to link the video account with goods? What are the requirements for a video link? How to link the video account with goods? Mar 07, 2024 pm 01:13 PM

What are the requirements for a video link? How to link the video account with goods?

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

Why NameResolutionError(self.host, self, e) from e and how to solve it

How to link on Doudian - Tutorial on linking on Doudian How to link on Doudian - Tutorial on linking on Doudian Mar 06, 2024 am 08:40 AM

How to link on Doudian - Tutorial on linking on Doudian

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

What is the difference between html and url What is the difference between html and url Mar 06, 2024 pm 03:06 PM

What is the difference between html and url

See all articles