JavaScript / ECMAScript

Positive Lookahead (Password) in JS

Use positive lookahead `(?=...)` to require the password contain at least one digit and one uppercase letter.

Try it in the JS tester →

Pattern

regexJS
^(?=.*\d)(?=.*[A-Z])[A-Za-z\d]{8,}$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^(?=.*\\d)(?=.*[A-Z])[A-Za-z\\d]{8,}$", "");
const input = "Password1";
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

(?=.*\d) is a zero-width assertion that requires the rest of the string to contain a digit somewhere. (?=.*[A-Z]) similarly requires an uppercase letter. Neither consumes characters. [A-Za-z\d]{8,}$ then matches the actual password content (≥8 alphanumerics). Lookaheads are supported in JS and Python; Go's RE2 does NOT support them.

Examples

Input

Password1

Matches

  • Password1

Input

weakpass

No match

Input

ALLCAPSNODIGIT

No match

Same pattern, other engines

← Back to Positive Lookahead (Password) overview (all engines)