PatternRegex

Match a hashtag

Find hashtags in captions, comments, or scraped posts — a # glued directly to letters, digits, or an underscore.

Last updated

Patterng
#[A-Za-z0-9_]+
Open in Regex Tester & Builder

Matches

  • Loving the #sunset tonight
  • New post tagged #web_dev2025

Doesn’t match

  • Price marked # 42 in the ledger
  • Use the number sign carefully please

Why it’s written this way

A literal # is immediately followed by [A-Za-z0-9_]+, one or more letters, digits, or underscores, which mirrors how most platforms define a valid tag body.

Because the character class requires at least one word character right after the #, a bare hash with nothing glued to it, or a hash followed by a space, never matches.

Edge cases to know

  • →It can't tell a social hashtag from a Markdown heading marker; '#Launch' at the start of a line matches the same way '#Launch' does inside a caption.
  • →Hex color codes like #fff or #a1b2c3 also satisfy the pattern, since hexadecimal digits are a subset of [A-Za-z0-9_] — worth filtering separately if you're scanning CSS or design files.
  • →Accented letters and punctuation-only tags fall outside [A-Za-z0-9_] and won't be found.

Part by part

#
The literal hash. No escaping needed — it is only special inside a character class or in extended-mode patterns.
[A-Za-z0-9_]+
One or more letters, digits or underscores. One character minimum, so a bare hash on its own does not match.

Common mistakes

  • #\w+

    Equivalent in most engines, but \w becomes unicode-aware under some configurations, which changes behaviour between languages and runtimes. Spelling the class out keeps the result identical everywhere.

  • #[A-Za-z0-9_]*

    A star allows a bare hash with nothing after it, so every heading marker in a Markdown document and every fragment in a URL registers as a hashtag.

    #[A-Za-z0-9_]+
  • #[A-Za-z0-9_-]+

    Adding the hyphen looks harmless and lets a match run into hyphenated words that follow a real tag, so "#news-worthy story" captures more than the tag itself.

Questions

Will it match a colour code or a Markdown heading?

Yes to both, and that is its main weakness. #ff0000 and # Heading are the same shape as a hashtag to this pattern. Requiring a boundary before the hash, so it cannot follow a word character, removes some but not all of these.

Does it match hashtags in other scripts?

No. The class is ASCII, so tags in Arabic, Hindi or Japanese are missed. Use [\p{L}\p{N}_]+ with the u flag for international text, which most platforms do support.

Can a hashtag start with a digit?

This pattern allows it, though several platforms reject all-numeric tags because they are indistinguishable from other identifiers. Filter them out afterwards if you are matching a specific platform's rules.

Are hashtags case-sensitive?

Not on the major platforms — they are matched case-insensitively while preserving the case you typed, which is why camel case is used to make multi-word tags readable. Lowercase before comparing or counting.

How do I stop it matching a hash inside a URL?

A URL fragment such as example.com#section has the same shape as a tag. Requiring that the hash is not preceded by a word character or a slash removes most of them, and matching URLs first and excluding those spans removes the rest — which is the more reliable of the two.

Related in Patterns