Text Processing

Positive Lookahead (Password)

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

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^(?=.*\\d)(?=.*[A-Z])[A-Za-z\\d]{8,}$", "");
const input = "Password1";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

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

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

How it 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

—

Common use cases

  • •Password complexity enforcement
  • •Form validation with multiple co-required tokens
  • •Filename validation (must end with extension AND start with letter)
  • •Compound-rule input filtering

Related patterns