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.
How the pattern works
- Unanchored by design — this is a find pattern: it pulls every occurrence out of longer text (use the
gflag in JavaScript). - Capture the tag without the hash:
#([A-Za-z0-9_]+)— group 1 is the word you store, count or deduplicate. - X/Twitter caps tags at 30 characters:
#[A-Za-z0-9_]{1,30}. Reject comma runs (#tag1,#tag2) explicitly if your source glues tags together. - Unicode tags (
#日本語,#café) need#[\p{L}\p{N}_]+— valid RE2 (\p{L}) and JavaScript with theuflag.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
Loving #MarketingTips today | Match | finds #MarketingTips | Try → |
#100DaysOfCode day3 | Match | digits allowed after the hash | Try → |
#two words | Match | matches #two — the space cuts it (see notes) | Try → |
C# is nice | No match | hash must be followed by a tag character | Try → |
just a hash # | No match | nothing after the # | Try → |
no tag here | No match | no hash at all | Try → |
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
- Hashes inside other contexts:
C#does not match (needs a tag character after), butissue#123does — decide whether bare #numbers are tags in your data. - Commas, periods and @ are excluded from the class, so
#tag,nextyields#tag— but LinkedIn allows hyphens in tags and this class does not; extend per platform. - Underscore-heavy runs match whole (
#not_a_file_name_) — trailing punctuation and emoji stop the match naturally, since neither is in the class.
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.
Related patterns
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.