Extract @mentions from text
Mentions start with @ followed by a username. Learn to extract mentions, validate usernames, and link them to user profiles.
- 1
A mention is @ followed by a username (letters, digits, underscores, hyphens). The regex @[\w]+ finds mentions. Some platforms allow dots (email-like). The @ must be at the start or preceded by whitespace (otherwise it is part of an email address). Validate extracted usernames against your user database.
- 2 Regex Tester →
Find all mentions with @\w[\w.]*\w or @\w+ (simpler). Test against text: "Thanks @alice and @bob_smith for the help! CC: @team.frontend". The tool shows all matches. Distinguish mentions from email addresses by checking for a preceding space or start of string.
- 3 Regex Replace →
Link mentions to profiles: replace @username with <a href="/users/username">@username</a>. Use a callback replacement to look up the user ID: replace @([\w]+) with a link to the user's profile if they exist, or leave as text if not found.
Frequently asked questions
- How do I distinguish mentions from email addresses?
- Check the context: if @ is preceded by a word character (no space), it is likely an email ([email protected]). If preceded by whitespace or start of string, it is a mention (@user). Use a lookbehind: (?<=\s|^)@\w+ or (?:^|\s)@(\w+) to match mentions only.
- What characters are allowed in usernames?
- Common: letters, digits, underscores ([a-zA-Z0-9_]). Some platforms allow hyphens and dots (but not consecutive dots or trailing dots). Check your platform's username rules. Validate extracted mentions against the user database — not all @word patterns are real users.
Workflows that solve this
Learn more
Want to understand why?