Match an @mention
Find @mentions in comments or chat logs, handy for building notifications, though email addresses need a guard.
Last updated
Matches
- Thanks @alice for the review
- cc @bob_dev on this
Doesn’t match
- Reach out via the contact form
- Just an at symbol alone here
Why it’s written this way
The @ is literal, and [A-Za-z0-9_]+ after it matches one or more username characters, covering the letters, digits, and underscore that platforms like GitHub, Slack, and X allow in a handle.
There's no check on what comes before the @, so it fires on any @ in the text that's followed by word characters, not only ones sitting at the start of a word.
Edge cases to know
- →The biggest gap: an email address like jane@example.com contains an @ followed by word characters, so this pattern matches '@example' inside it as though it were a mention. Guarding against that needs a negative lookbehind for a preceding word character, such as (?<!\w)@[A-Za-z0-9_]+, which refuses to start the match when the @ is glued onto an existing word.
- →It doesn't allow dots or hyphens in the handle, so a platform that permits those in usernames would need a wider character class.
Part by part
- @
- The literal at-sign. It carries no special meaning in regex and needs no escaping.
- [A-Za-z0-9_]+
- The username: letters, digits and underscores, at least one. This matches the character set most platforms allow.
Common mistakes
@[A-Za-z0-9_.-]+Adding the dot and hyphen makes email addresses match, so scanning a document returns the domain of every address as though it were a mention.
@\w+Works, but \w varies with unicode settings across engines, so the same pattern can match a different set of characters depending on where it runs. The explicit class is predictable.
@[A-Za-z0-9_]*A star matches a bare at-sign, so every email address in the text produces a zero-length hit at its separator and the result is full of empty matches.
@[A-Za-z0-9_]+
Questions
Will it match the username inside an email address?
It matches the part after the at-sign, so bob@example.com yields a hit on example. Require a boundary before the @ so it cannot follow a word character, which excludes addresses while keeping real mentions.
Does it handle mentions with a domain, like @user@server?
It matches only the first part. Federated networks such as Mastodon use that longer form, which needs an optional second segment appended to cover both local and remote handles.
Should I validate that the user exists?
The pattern cannot, and treating a match as a real account is how notification systems end up messaging nobody. Look the handle up before acting on it, and decide deliberately what to do when it does not resolve.
How long can a username be?
This pattern sets no limit, and platforms vary from fifteen characters upward. Add a bound such as {1,15} if you are matching one platform's rules rather than finding mention-shaped text in general.
Should I notify everyone a match resolves to?
Be careful. Text quoted from elsewhere, a code sample containing an at-sign, and an address the boundary check missed all look identical to a deliberate mention. Notifying on every match is how a system sends alerts nobody asked for, so confirm the mention was authored rather than quoted.