Twitter / X URL
Match Twitter/X profile and status URLs, capturing the handle and (optional) tweet ID.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
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).
Python (re) code
import re
pattern = re.compile(r"https?:\/\/(?:www\.)?(?:twitter|x)\.com\/([A-Za-z0-9_]{1,15})(?:\/status\/(\d+))?")
input_text = "https://twitter.com/jack/status/20"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`https?:\/\/(?:www\.)?(?:twitter|x)\.com\/([A-Za-z0-9_]{1,15})(?:\/status\/(\d+))?`)
input := `https://twitter.com/jack/status/20`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
https?:\/\/(?:www\.)?(?:twitter|x)\.com\/([A-Za-z0-9_]{1,15})(?:\/status\/(\d+))? (flags: g)Raw source: https?:\/\/(?:www\.)?(?:twitter|x)\.com\/([A-Za-z0-9_]{1,15})(?:\/status\/(\d+))?
How it works
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
—Common use cases
- •Embedding tweets in CMS content
- •Social media analytics — extracting tweet IDs from text
- •Migrating links from twitter.com to x.com
- •Citation collection in news / research workflows
Related patterns
LinkedIn Profile URL
WebMatch LinkedIn profile URLs and capture the profile slug.
URL Validation
WebMatch http and https URLs with optional www prefix, paths, query strings, and fragments.
GitHub Repository URL
WebMatch GitHub repository URLs and capture the owner and repo segments.
URL Path Segment
WebMatch individual `/segment` parts of a URL path, capturing each one.