Username Regex

^[a-zA-Z0-9_]{3,16}$

Validate usernames: 3-16 characters, alphanumeric with underscores allowed.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^[a-zA-Z0-9_]{3,16}$:

  • ^ - Start of string
  • $ - End of string
  • [...] - Character class
  • {n,m} - Quantifier (n to m times)

Examples

Input Result Explanation
john_doe ✓ Match Valid username
user123 ✓ Match Alphanumeric
ab ✗ No Match Too short
user-name ✗ No Match Hyphen not allowed

Code Examples

JavaScript

JavaScript
const pattern = /^[a-zA-Z0-9_]{3,16}$/;
const text = "john_doe";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^[a-zA-Z0-9_]{3,16}$'
text = "john_doe"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^[a-zA-Z0-9_]{3,16}$/';
$text = "john_doe";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^[a-zA-Z0-9_]{3,16}$";
String text = "john_doe";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: User registration, social platforms, gaming

Frequently Asked Questions

What is the regex pattern for username regex?
The regex pattern is: ^[a-zA-Z0-9_]{3,16}$. Validate usernames: 3-16 characters, alphanumeric with underscores allowed.
How do I use the username regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^[a-zA-Z0-9_]{3,16}$'); or const regex = /^[a-zA-Z0-9_]{3,16}$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^[a-zA-Z0-9_]{3,16}$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for username regex?
User registration, social platforms, gaming

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: