BytePane

Username Regex Pattern

Validates usernames with letters, numbers, underscores, and hyphens. Length 3-20 characters.

T
Text & Strings
Beginner
Difficulty
Universal
Language
none
Flags
// Regular Expression
/^[a-zA-Z0-9_-]{3,20}$/

Live Regex Tester

Pattern Breakdown

^[a-zA-Z0-9_-]{3,20}$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /^[a-zA-Z0-9_-]{3,20}$/;
const test = "cool_user123";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'^[a-zA-Z0-9_-]{3,20}$'
test = "cool_user123"
match = re.search(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^[a-zA-Z0-9_-]{3,20}$`)
    fmt.Println(re.MatchString("cool_user123")) // true
}

Common Use Cases

User registrationprofile validationsocial media handles

Match Examples

InputResult
cool_user123Match
abNo Match

About the Username Regex

Validates usernames with letters, numbers, underscores, and hyphens. Length 3-20 characters.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The username pattern is classified as beginner difficulty in the text & strings category. It works in all major programming languages.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Username regex pattern?

Validates usernames with letters, numbers, underscores, and hyphens. Length 3-20 characters.

How do I use the Username regex?

Use the pattern /^[a-zA-Z0-9_-]{3,20}$/ in your code. In JavaScript: new RegExp('^[a-zA-Z0-9_-]{3,20}$', ''). Test it above with your own input.

What does this Username regex match?

This pattern matches: "cool_user123". It does NOT match: "ab". User registration, profile validation, social media handles.

Is the Username regex beginner-friendly?

This pattern is rated Beginner. It uses basic regex syntax and is easy to understand.

What languages support the Username regex?

This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.

Can I modify the Username regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools