Social
Hashtag Regex
Hashtags group posts by topic on almost every social platform. This pattern finds a # sign at the start of the text or after whitespace, followed by a letter or underscore and then any run of letters, digits and underscores. Requiring whitespace before the # stops it from matching URL fragments and references like issue#42, and requiring a letter first skips pure numbers such as #2026, which most platforms do not link. It is ideal for tagging, analytics and building tag clouds from captions. Its main limit is that it only covers ASCII letters, so hashtags written in other scripts need a Unicode-aware version.
Pattern (JavaScript)
/(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*/Matches (the start of the line/string OR a whitespace character (space, tab, newline)), then the character "#", then one of the characters A-Za-z_, then one of the characters A-Za-z0-9_, zero or more times.
Examples
Matches
- ✓ Loving the #regex tips
- ✓ #launch_day
- ✓ Tags: #a1 #b2
- ✓ New post #_draft
Does not match
- ✗ issue#42 is fixed
- ✗ # heading
- ✗ #2026
- ✗ no tags here
Try it
5 matches (JavaScript engine)
Loving the #regex tips #launch_day Tags: #a1 #b2 New post #_draft issue#42 is fixed # heading #2026 no tags here
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*/g;
const text = "Loving the #regex tips";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*')
text = 'Loving the #regex tips'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*/';
$text = 'Loving the #regex tips';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?:^|\\s)#[A-Za-z_][A-Za-z0-9_]*");
String text = "Loving the #regex tips";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*`)
text := "Loving the #regex tips"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*");
var text = "Loving the #regex tips";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*").unwrap();
let text = "Loving the #regex tips";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /(?:^|\s)#[A-Za-z_][A-Za-z0-9_]*/
text = 'Loving the #regex tips'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is #2026 not treated as a hashtag?
- Most platforms do not link hashtags made only of digits, because they collide with numbering like #1 or issue numbers. The pattern requires a letter or underscore right after the # sign for the same reason.
- How do I get the tag text without the leading space?
- Wrap the part after the # in a capture group, for example #([A-Za-z_][A-Za-z0-9_]*), and read group 1. With the g flag and matchAll you get every tag in a caption.
- Does this support hashtags in Arabic or other scripts?
- No. The character classes cover ASCII letters only. Replace them with \p{L} and \p{N} and add the u flag in JavaScript if you need hashtags written in other alphabets.