Aller au contenu
uencoder

Extraire @mentions from text

Mentions démarrer avec @ followed par un username. Apprendre en extraire mentions, valider usernames, et link them en utilisateur profiles.

  1. 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. 2
    Testeur 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. 3
    Remplacement par 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.

Questions fréquentes

How do I distinguish mentions from email addresses?
Vérifier 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 résolvant cette tâche

Outils associés