Social
X (Twitter) handle Regex
Handles on X, the platform formerly known as Twitter, are made of letters, digits and underscores and can be at most 15 characters long. New accounts must use at least four characters. This pattern checks exactly that, with an optional @ so users can type the handle either way in a profile form. It is useful for sign-up forms, social link fields and cleaning exported follower lists. Older accounts created before the four character minimum can have shorter handles and will be rejected here, so relax the lower bound if you deal with legacy data. It cannot tell you whether the account exists.
Pattern (JavaScript)
/^@?[A-Za-z0-9_]{4,15}$/Starts at the start of the line/string, then the character "@", optionally (zero or one time), then one of the characters A-Za-z0-9_, between 4 and 15 times, then the end of the line/string.
Examples
Matches
- ✓ @test_user
- ✓ test_user
- ✓ @Jack_Test99
- ✓ @abcd
Does not match
- ✗ @ab
- ✗ @this_handle_is_way_too_long
- ✗ @test-user
- ✗ @test user
Try it
0 matches (JavaScript engine)
@test_user
test_user
@Jack_Test99
@abcd
@ab
@this_handle_is_way_too_long
@test-user
@test userCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^@?[A-Za-z0-9_]{4,15}$/g;
const text = "@test_user";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^@?[A-Za-z0-9_]{4,15}$')
text = '@test_user'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^@?[A-Za-z0-9_]{4,15}$/';
$text = '@test_user';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^@?[A-Za-z0-9_]{4,15}$");
String text = "@test_user";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^@?[A-Za-z0-9_]{4,15}$`)
text := "@test_user"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^@?[A-Za-z0-9_]{4,15}$");
var text = "@test_user";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^@?[A-Za-z0-9_]{4,15}$").unwrap();
let text = "@test_user";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^@?[A-Za-z0-9_]{4,15}$/
text = '@test_user'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is the minimum four characters?
- X requires new handles to be at least four characters. Some old accounts still have shorter handles, so change {4,15} to {1,15} if you need to accept those legacy names.
- Can X handles contain dots or dashes?
- No. Only letters, digits and underscores are allowed, which is why test-user and test.user are rejected. The pattern mirrors the platform's own rule.
- Are handles case-sensitive?
- No. X treats @Test_User and @test_user as the same account. Compare handles in lowercase when checking for duplicates, even though the pattern accepts both cases.