Regex to Match a Hashtag
Copy the pattern, paste your text below, and see every hashtag it finds. Below the tester: why it skips number-only tags and how to allow Unicode.
Regex pattern
/(?<!\w)#[A-Za-z_]\w*/g
This matches a hashtag that starts with a letter or underscore followed by word characters, and the lookbehind prevents matching inside a longer word. It does not match hashtags that begin with a digit.
What this pattern matches
This matches a hashtag: a # that starts a word, a first character that is a letter or underscore, and then any word characters. Number-only tags like #100 are skipped.
How it works, part by part
(?<!\w) — a lookbehind so the # is not preceded by a word character (skips a#b).
#[A-Za-z_] — the hash and a required first letter or underscore.
\w* — the rest of the tag: letters, digits, or underscores.
Unicode hashtags
To match non-Latin tags like #café or #日本, use the Unicode flag and property classes (\p{L}) where your engine supports them.
Use it in your code
import re
text = "Loving #Regex and #dev2026! Prices like #100 are ignored, and a mid-word a#b is skipped."
pattern = r'''(?<!\w)#[A-Za-z_]\w*'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Loving #Regex and #dev2026! Prices like #100 are ignored, and a mid-word a#b is skipped.";
const re = new RegExp(String.raw`(?<!\w)#[A-Za-z_]\w*`, "g");
console.log(text.match(re));
FAQ
Why is #100 ignored?
The first character after # must be a letter or underscore, so a purely numeric tag does not match.
Does it match a # in the middle of a word?
No. The lookbehind requires the # to start a word, so a#b is skipped.
How do I match emoji or non-Latin hashtags?
Use the u flag with \p{L} character properties in engines that support Unicode property escapes.