Advertisement

Pattern library / URLs & Marketing

Hashtag Regex (#words)

A hashtag is # followed by letters, digits or underscores — #MarketingTips, #100DaysOfCode. The pattern is short; the interesting part is the edges: C# is not a tag, spaces cut tags in half, and platform rules (X's 30-character cap, unicode scripts) decide how far you should let it run.

The pattern
#[A-Za-z0-9_]+
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
Loving #MarketingTips todayMatchfinds #MarketingTipsTry →
#100DaysOfCode day3Matchdigits allowed after the hashTry →
#two wordsMatchmatches #two — the space cuts it (see notes)Try →
C# is niceNo matchhash must be followed by a tag characterTry →
just a hash #No matchnothing after the #Try →
no tag hereNo matchno hash at allTry →

Where to use it

JavaScript

const tags = text.match(/#[A-Za-z0-9_]+/g) || []; // all hashtags
const first = text.match(/#([A-Za-z0-9_]+)/);       // first[1] = tag word

Python

import re
TAG = r'#([A-Za-z0-9_]+)'
tags = re.findall(TAG, 'Loving #MarketingTips #Day3')  # ['MarketingTips', 'Day3']

BigQuery / GA4 (RE2)

SELECT REGEXP_EXTRACT_ALL(text, r'#[A-Za-z0-9_]+') AS hashtags
-- or check one campaign tag:
-- WHERE REGEXP_CONTAINS(text, r'#SummerSale\b')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I extract all hashtags from a string?

Use the find form with the global flag: text.match(/#[A-Za-z0-9_]+/g) in JavaScript, re.findall(r'#[A-Za-z0-9_]+', text) in Python.

Why does C# count as a hashtag?

It does not with this pattern — C# followed by a space fails [A-Za-z0-9_]+. issue#123 does match (#123); add (^|\s) before the hash if your source needs start-of-word semantics.

How do I match unicode hashtags like #café?

Switch the class to Unicode properties: #[\p{L}\p{N}_]+ with the u flag in JavaScript — RE2 (BigQuery, GA4, Go) supports \p{L} natively.

Mention Regex - Match @handles in Text · URL Regex — Match http/https Links (Practical) · UTM Parameter Regex — Match & Extract utm_* Tags

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.