Skip to main content
Authentication & Security · In-Browser Regular Expression Lab

Username Regex — 3 to 20 Characters with Letters, Digits & Underscores

Standard web application username policy: 3 to 20 characters, starting with a letter, allowing underscores.

Flags:
//none
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:alex_dev

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

alex_dev Pass
sarah99 Pass
CastovUser Pass

Expected Invalid Samples (Must Reject)

9alex Rejected (Pass)
_user Rejected (Pass)
us Rejected (Pass)
this_username_is_way_too_long_for_policy Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/;
const text = "alex_dev";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Begins with an ASCII letter, followed by 2 to 19 alphanumeric characters or underscores. Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
^[a-zA-Z]First LetterMust start with a letter (no leading numbers or underscores).
[a-zA-Z0-9_]{2,19}$Trailing Chars2 to 19 characters total length 3-20.

Frequently Asked Questions

How to allow hyphens?
Change character set to `[a-zA-Z0-9_-]`.
Browse All 40 Patterns