Extraer @mentions from text
Mentions iniciar con @ followed por un username. Aprender a extraer mentions, validar usernames, y link them a usuario 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 Probador de regex →
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 Reemplazo con regex →
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.
Preguntas frecuentes
- How do I distinguish mentions from email addresses?
- Verificar 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.