Regex tester / pattern library

Regex: username

/^[a-z0-9](?:[a-z0-9_-]{1,18}[a-z0-9])?$/i Test it live

How it works

3–20 characters overall shape (1 + up-to-18 + 1): starts and ends alphanumeric, allows _ and - in the middle. The (?:…)? makes a single-character username also valid — tighten to your policy.

Honest caveats

Every service has its own rules (GitHub forbids consecutive hyphens, Twitter allows leading underscores). Treat this as a template: adjust the length bounds and allowed separators to your policy, and always check reserved names separately.

Matches

  • stormrider
  • user_42
  • a-b-c

Doesn't match

  • _leading
  • trailing-
  • a
  • name with spaces

More patterns