Regex To Match All #hashtags In A String

Category: Strings

Searching for hashtags can be quite frustrating. It seems like usually, it would be easy to find every hashtag by looking at the string of characters. But what if you have a very long string with hundreds of words? Is it possible to match all the hashtags in that string?

Here’s a handy regular expression that can be used to match all #hashtags in strings, like Twitter tweets and Facebook posts. Enjoy.

/(#\w+)/g

Matches:

  • This is a #hashtag.
  • #This is a #hashtag.

Non-matches:

  • This is a @hashtag.
  • This is a hashtag#

See Also:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.