Regex to Match Hashtags
Match social media hashtags with this regex pattern. Captures #hashtag patterns that start with a letter, commonly used on Twitter and Instagram.
Regular Expression
/#[a-zA-Z]\w{0,138}/g
Token Breakdown
| Token | Description |
|---|---|
| # | Matches the literal character '#' |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| \w | Matches any word character (letter, digit, underscore) |
| {0,138} | Matches between 0 and 138 times |
Detailed Explanation
This regex matches social media hashtags as used on platforms like Twitter, Instagram, and TikTok. Here is the token-by-token breakdown:
— Matches the literal hash (pound) sign that begins every hashtag. The # character is not a regex metacharacter, so no escaping is needed.
[a-zA-Z] — A character class matching a single letter (upper or lowercase) immediately after the hash. Hashtags must start with a letter, not a digit or underscore. This prevents matching color codes like #123 or numbered items like #1.
\w{0,138} — Matches zero to 138 additional word characters (letters, digits, and underscores). The \w shorthand is equivalent to [a-zA-Z0-9_]. The upper limit of 138 combined with the initial letter gives a maximum hashtag length of 140 characters (matching Twitter's original character limit). Most hashtags are much shorter.
The g flag enables global matching to find all hashtags in the text. This pattern matches hashtags like #JavaScript, #coding, #100DaysOfCode, #web_dev, and #AI. It does not match bare hash symbols, numeric references like #42, or hex color codes like #FF5733 (because those start with digits).
Note that this pattern follows ASCII conventions. Real-world hashtags on platforms like Twitter can include Unicode characters from many scripts. For international hashtag support, replace the character classes with Unicode-aware equivalents. The pattern also does not validate whether a hashtag is meaningful or used on any specific platform.
Example Test Strings
| Input | Expected |
|---|---|
| #JavaScript | Match |
| #100DaysOfCode | No Match |
| #coding | Match |
| no hashtag | No Match |
| #web_dev | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
18 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex