jquery analyzes the way url or email addresses in text are real links

  • 2020-06-19 09:41:35
  • OfStack

This article gives an example of how jquery can analyze text in which url or email addresses are real links. Share to everybody for everybody reference. The details are as follows:

This code analyzes all the hyperlinks in the text, including email, url, # links, and so on, and outputs the actual link addresses


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

Usage:

$("p").tweetify();

Original text:

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

After analysis:


<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>

I hope this article has been helpful for your jQuery programming.


Related articles: