JavaScript / ECMAScript

Twitter / X Handle in JS

Match Twitter/X @handles — 1 to 15 characters of letters, digits, or underscores preceded by @.

Try it in the JS tester →

Pattern

regexJS
@([A-Za-z0-9_]{1,15})\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("@([A-Za-z0-9_]{1,15})\\b", "g");
const input = "Follow @jack and @TwitterDev for updates";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

@ matches the literal at-sign. The capturing group ([A-Za-z0-9_]{1,15}) captures 1–15 characters of the Twitter username alphabet. \b prevents partial matches inside longer words.

Examples

Input

Follow @jack and @TwitterDev for updates

Matches

  • @jack
  • @TwitterDev

Input

@user_123 liked your post

Matches

  • @user_123

Input

no handles here

No match

Same pattern, other engines

← Back to Twitter / X Handle overview (all engines)