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/20Matches
https://twitter.com/jack/status/20
Input
Profile: https://x.com/elonmuskMatches
https://x.com/elonmusk
Input
no twitter linksNo match
—