Regex for Matching an @ Mention
Matches an @ symbol followed by a username made of word characters.
Published October 12, 2026
This regex matches @mentions: an @ followed by a username of letters, digits or underscores, not preceded by a word character (so emails are skipped).
How the pattern works
Breaking the pattern down: (?<![\w@]) = not preceded by a word character; @\w+ = at sign and username.
Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.
Pattern tests
/(?<![\w@])@\w+/
✓ 'hi @ada'
✓ '@bob_1'
✗ 'email@host.com'
✗ 'no mention'
Practical examples
The pattern
const re = /(?<![\w@])@\w+/Test it
const re = /(?<![\w@])@\w+/
re.test('hi @ada') // true
re.test('@bob_1') // true
re.test('email@host.com') // false
re.test('no mention') // falseIn Python
import re
re.search(r'(?<![\w@])@\w+', text)Common mistakes
- Matching inside email addresses.
- Lookbehind support in old browsers.
- Username length limits.
Key takeaways
- Anchors (^ and $) make the whole string match instead of a part of it.
- Regex checks format only; verify data separately when it matters.
- Test with valid and invalid samples.
FAQ
Safari support for lookbehind?
Modern versions support it; older ones do not.
Where can I test this pattern?
Use the Regex Tester on this site, or regex101.com.