Social
@mention Regex
Mentions notify a user that they have been named in a post, comment or chat message. This pattern finds an @ sign at the start of the text or after a character that is not a letter, digit, dot or another @, followed by one to thirty letters, digits or underscores. That boundary rule is what keeps it from firing on the middle of an email address such as user@example.com. Use it to highlight mentions in a chat app, build notification lists or count how often accounts are tagged. It does not check whether the account exists, and platform-specific length limits differ.
Pattern (JavaScript)
/(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b/Matches (the start of the line/string OR any character except \w@.), then the character "@", then one of the characters A-Za-z0-9_, between 1 and 30 times, then a word boundary.
Examples
Matches
- ✓ Thanks @test_user!
- ✓ @dev_team please review
- ✓ cc: @a1
- ✓ (@sample_account)
Does not match
- ✗ email user@example.com
- ✗ @ test
- ✗ no mentions here
- ✗ a@@b
Try it
4 matches (JavaScript engine)
Thanks @test_user! @dev_team please review cc: @a1 (@sample_account) email user@example.com @ test no mentions here a@@b
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b/g;
const text = "Thanks @test_user!";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b')
text = 'Thanks @test_user!'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b/';
$text = 'Thanks @test_user!';
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("(?:^|[^\\w@.])@[A-Za-z0-9_]{1,30}\\b");
String text = "Thanks @test_user!";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b`)
text := "Thanks @test_user!"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b");
var text = "Thanks @test_user!";
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"(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b").unwrap();
let text = "Thanks @test_user!";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b/
text = 'Thanks @test_user!'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How does the pattern avoid matching email addresses?
- It requires the @ to be at the start of the text or preceded by a character that is not a word character, dot or @. In an email address the @ always follows a letter or digit, so it is skipped.
- Why is the length capped at 30 characters?
- Thirty covers the username limits of the major platforms, including X at 15 and Instagram at 30. Lower the upper bound if you only handle one platform with a stricter limit.
- Will it match handles with dots, like @test.user?
- It matches the @test part and stops at the dot, because dots are not in the character class. Add a dot to the class if your platform allows dots inside usernames.