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.

Related in Patterns