Twitter Links Regex

The Twitter API returns tweets as a plain string with no HTML tags. Granted the Twitter API isn’t designed to be used exclusively for websites, other programing languages / technologies can handle this in their own way. A tweet can contain three types of links; urls, hashtags and mentions.

Urls start with http or https, hashtags start with a # symbol and mentions start with an @ symbol. To convert the string to HTML, I’ve used a series of Regexes. They identify the type of link and apply an anchor tag in its place with the correct href context and a class for styling.