Search code examples
javascriptjquerytwittertwitter-feed

how do I find a # with jQuery?


I recently built a small app for my site that allows my users to display their tweets. I am wondering if their is a way with jQuery or Javascript to detect or find stuff like hashtags and http://'s ?

A simple example of a sentence might be:

The quick #brown fox jumps over the #lazy dog

Solution

  • You might give twitter-text-js a try. It can do things like auto link @mentions, #hashtags, and urls.

    twttr.txt.autoLink('A @screen_name, #hashtag, and url: http://twitter.com autolinked')
    

    Will result in:

    "A @<a class="tweet-url username" data-screen-name="screen_name" href="http://twitter.com/screen_name" rel="nofollow">screen_name</a>, <a href="http://twitter.com/search?q=%23hashtag" title="#hashtag" class="tweet-url hashtag" rel="nofollow">#hashtag</a>, and url: <a href="http://twitter.com" rel="nofollow" >http://twitter.com</a> autolinked"
    

    Or finding the indices of #hashtags:

    twttr.txt.extractHashtagsWithIndices('A @screen_name, #hashtag, and url: http://twitter.com autolinked')
    

    will result in:

    [{
      hashtag: "hashtag",
      indices: [ 16, 24 ]
    }]