The twitter plugin has just been upgraded to (optionally) search for links in the twitter text and activate them as link.

So I thought I'd share the linkify code.

Proudly sponsored by
CodeSponsor.io- get paid by adding one line of code to your README
String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:% \?\/.=]+[^\.,\)\s*$]/g, function(m) {
    return m.link(m);
  });
};

Short and sweet - will return a string with the URLs as links. Note that if the linkify function encounters a real link, it will break the link - ideally use this code with text you know may contain a plain text link.

For example, assuming status is an element on the page, and twitter.text is our text to search for links within:

status.innerHTML = twitter.text.linkify();

The function has been tested in IE7, Firefox 2, Safari 2 and Opera 9. It has also been tested with domains with ports, paths and query strings.

I could see this being turned in to a bookmarklet to scan the current page to enable links, for instance in a forum where the link has been pasted in and not been converted (though the regex would need a tweak, or something altogether smarter may be required).

Let me know if you have any questions or feedback in case of mistakes.

Want more?

Posts, web development learnings & insights, exclusive workshop and training discounts and more, direct to your inbox.

I won't send you any spam, and you can unsubscribe at any time. Powered by ConvertKit