Skip to content

Instantly share code, notes, and snippets.

@andyoakley
Created January 21, 2012 07:05
Show Gist options
  • Save andyoakley/1651851 to your computer and use it in GitHub Desktop.
Save andyoakley/1651851 to your computer and use it in GitHub Desktop.
Include tweets into HTML via javascript
var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img;
var url_replace = '$1$4$7$10$13<a href="$2$5$8$11$14">$2$5$8$11$14</a>$3$6$9$12';
requestURL = "http://api.twitter.com/1/statuses/user_timeline/apoakley.json?count=5&include_rts=t&callback=?";
$.getJSON(requestURL, function(json, status){
var content = "";
$.each(json, function(i){
tweet = this['text'].replace(url_pattern, url_replace);
content += "<li class=\"tweets\">" + tweet + "</li>";
});
$("ul#tweets").html(content);
});
@andyoakley
Copy link
Author

Credit goes to Jeff Roberson for the 'linkify' regex. http://jmrware.com/articles/2010/linkifyurl/linkify.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment