Mention Handle Regex
Validates a social-media style @mention handle: an @ symbol followed by 1 to 15 letters, digits, or underscores, matching common username length limits.
Regex Pattern
^@[A-Za-z0-9_]{1,15}$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| @ | Literal at-sign that begins every mention handle |
| [A-Za-z0-9_] | Character class allowing letters, digits, and underscores |
| {1,15} | Requires between 1 and 15 of the allowed characters, mirroring common platform handle-length limits |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern checks that a string is a single, complete @mention handle: an @ symbol followed by 1 to 15 alphanumeric or underscore characters, with nothing else before or after.
Why it works
The `{1,15}` bound directly encodes the maximum handle length used by several major social platforms, so both too-short (bare @) and too-long handles are rejected in one quantifier. Restricting the body to `[A-Za-z0-9_]` excludes spaces, punctuation, and hyphens, which keeps the handle a single unbroken token, and the `^`/`$` anchors ensure the entire input is the handle rather than a mention embedded inside a longer string.
Common use cases
- Validating a single @mention entered into a compose box's tag-a-user field
- Checking that autocomplete or lookup input for user handles is well-formed before hitting an API
- Filtering a list of extracted mentions to discard malformed or over-length entries
- Server-side validation of handles submitted via a public API before database lookup
Edge cases
- A bare @ with nothing after it is correctly rejected as an empty handle
- Handles at exactly the 15-character boundary, like @exactly_fifteen, are matched, while 16 characters is rejected
- Handles containing a leading digit, like @1username, are accepted since digits are allowed anywhere in the body
- Handles with a dot or hyphen, common on some platforms for display names, are rejected since only letters, digits, and underscores are permitted
Limitations
- Does not extract multiple @mentions from a larger block of free text; use a global, unanchored variant for that
- The 15-character limit is specific to certain platforms and may need adjusting for others with different handle-length rules
- Does not verify that the mentioned user account actually exists
- Does not support Unicode letters for platforms that allow non-ASCII characters in handles
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const mentionRegex = /^@[A-Za-z0-9_]{1,15}$/;
console.log(mentionRegex.test('@jack')); // trueCommon Mistakes
Using an unbounded `+` for the handle body, which allows arbitrarily long strings that violate real platform limits
Fix: Bound the quantifier explicitly, such as `{1,15}`, to match the target platform's actual handle-length rule
Allowing hyphens or dots in the handle body, which many mention systems disallow
Fix: Restrict the character class to `[A-Za-z0-9_]` unless the target platform explicitly permits other characters
Trying to use this anchored single-handle pattern to pull every mention out of a block of text
Fix: Remove the `^`/`$` anchors and add the `g` flag to find all `@handle` occurrences within free-form text
Performance Notes
- The bounded `{1,15}` quantifier keeps the maximum match length small and predictable, avoiding any risk of runaway backtracking
- Anchoring with `^` and `$` allows the engine to fail fast on strings that do not begin with @
- For bulk validation of many handles, compile the regex once and reuse the same instance across calls
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |