Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex: Hashtags
Regex pattern · find & extract

Regex: Hashtags

Regex for hashtags: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /(?<!\w)#\w+/g with the global flag to find every match. In "Loving #regex and #JS_2026! issue#42 is not a tag." it finds "#regex", "#JS_2026". \w is ASCII-only in JavaScript, so #café stops at #caf.

Last updated · Maintained by FreeAIToolz · How we test

/(?<!\w)#\w+/g

2 match(es)

Loving #regex and #JS_2026! issue#42 is not a tag.
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(?<!\w)not preceded by a letter or digit, so issue#42 is skipped
#hash sign
\w+letters, digits or underscores

Flags: g finds every match instead of stopping at the first.

Examples

InputMatches
Loving #regex and #JS_2026! issue#42 is not a tag.#regex, #JS_2026

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

\w is ASCII-only in JavaScript, so #café stops at #caf. Use [\p{L}\p{N}_]+ with the u flag for international hashtags.

When to use this pattern

Extracting hashtags from social posts for analytics or tagging.

Use it in your language

JavaScript

const matches = text.match(/(?<!\w)#\w+/g) ?? [];

Python

import re
matches = [m.group(0) for m in re.finditer(r'(?<!\w)#\w+', text)]

PHP

preg_match_all('~(?<!\\w)#\\w+~', $text, $m);
$matches = $m[0];

Java

Matcher m = Pattern.compile("(?<!\\w)#\\w+").matcher(text);
while (m.find()) { System.out.println(m.group()); }

Go’s regexp package (RE2) does not support lookahead or lookbehind, so this exact pattern will not compile in Go.

More regex patterns

Frequently asked questions

What is the regex for hashtags?
/(?<!\w)#\w+/g. \w is ASCII-only in JavaScript, so #café stops at #caf. Use [\p{L}\p{N}_]+ with the u flag for international hashtags.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It does not work in Go, because RE2 has no lookaround support.
How do I replace the matches?
In JavaScript use text.replace(/(?<!\w)#\w+/g, "replacement"); in Python use re.sub(r'(?<!\w)#\w+', "replacement", text).