Regex To Match All #hashtags In A String

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:

Regex Is Copied!