James Wilson
James Wilson

Reputation: 809

How do I return tweets with a certain hash tag with this JavaScript?

filter: function(t){ return  /^@\w+/.test(t.tweet_raw_text); },

If this JS returns tweets that start with an @ symbol, how to do return tweets with a specific hash tag, or word in them?

Everything I try just breaks it! It originates from this JS:

http://tweet.seaofclouds.com/

Upvotes: 1

Views: 194

Answers (2)

James Wilson
James Wilson

Reputation: 809

Thank your the above reply - a good lesson in reg expressions.

I also got round the problem with this code:

filter: function(t){ if (t.tweet_raw_text.indexOf("#EF1") !== -1; },

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 406135

First let's break down the regular expression you have to see how it works.

/^@\w+/ - The slashes (/) at the beginning and end are just delimiters that tell JavaScript that this is a regular expression.

^ - matches the beginning of a string.
@ - matches the literal @ symbol.
\w - matches any alphanumeric character including underscore (short for [a-zA-Z0-9_]).
+ - is short for {1,}. Matches the previous character or expression (\w) one or more times.

That's how you match a tweet that starts with the @ symbol. To match a tweet that contains a specific hashtag, you can replace the regular expression above with the specific hashtag you're trying to match.

For example, /#StackOverflow/.test(t.tweet_raw_text); will match a tweet that contains the exact hashtag #StackOverflow. That's a case-sensitive pattern though, so it wouldn't match the hashtag #stackoverflow. To make a JavaScript regular expression case insensitive, just add the i modifier after the closing delimeter like so: /#StackOverflow/i.

Upvotes: 2

Related Questions