Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Created November 4, 2011 15:46
Show Gist options
  • Save joshuabaker/1339648 to your computer and use it in GitHub Desktop.
Save joshuabaker/1339648 to your computer and use it in GitHub Desktop.
Parse Tweet
function parseTweet(str) {
return str
.replace(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi, '<a href="$1" target="_blank" class="tweet-link">$1</a>')
.replace(/(^|[\W])@(\w+)/gi, '$1@<a href="http://twitter.com/$2">$2</a>')
.replace(/\#([a-zA-Z0-9-_]+)/gi, '<a href="http://twitter.com/search/%23$1" target="_blank" class="tweet-hash">#$1</a>');
}
@joshuabaker
Copy link
Author

This method should be considered deprecated. The use of Twitter entities is encouraged.

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