JavaScript / ECMAScript

Username in JS

Validate usernames that are 3–16 characters long and contain only letters, digits, underscores, and hyphens.

Try it in the JS tester →

Pattern

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

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[a-zA-Z0-9_\\-]{3,16}$", "");
const input = "john_doe";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

^ and $ anchor to the full string. The character class [a-zA-Z0-9_\-] allows letters (upper and lower), digits, underscores, and hyphens. {3,16} enforces the length range most platforms use for usernames.

Examples

Input

john_doe

Matches

  • john_doe

Input

user-123

Matches

  • user-123

Input

ab

No match

Same pattern, other engines

← Back to Username overview (all engines)