JavaScript / ECMAScript

Twitter / X URL in JS

Match Twitter/X profile and status URLs, capturing the handle and (optional) tweet ID.

Try it in the JS tester →

Pattern

regexJS
https?:\/\/(?:www\.)?(?:twitter|x)\.com\/([A-Za-z0-9_]{1,15})(?:\/status\/(\d+))?   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("https?:\\/\\/(?:www\\.)?(?:twitter|x)\\.com\\/([A-Za-z0-9_]{1,15})(?:\\/status\\/(\\d+))?", "g");
const input = "https://twitter.com/jack/status/20";
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

https?:\/\/(?:www\.)?(?:twitter|x)\.com matches the domain — both legacy twitter.com and current x.com, with optional www. ([A-Za-z0-9_]{1,15}) captures the handle. (?:\/status\/(\d+))? optionally captures a tweet ID for status URLs.

Examples

Input

https://twitter.com/jack/status/20

Matches

  • https://twitter.com/jack/status/20

Input

Profile: https://x.com/elonmusk

Matches

  • https://x.com/elonmusk

Input

no twitter links

No match

Same pattern, other engines

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