Regex for password rules

The classic "at least one uppercase letter, one number and one symbol" regex, how its lookaheads work, and why current guidance from NIST tells you to check length and a blocklist instead.

//

Matches

This tool runs in your browser. We never send what you type or paste to a server.

The pattern

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/

Each (?=…) is a lookahead that checks one rule from the start of the string without consuming anything, and only the final .{8,} matches the password itself. That is why the rules can be listed in any order and still apply to the whole string.

The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.

How it works, token by token

PartMeaning
^Start of the string.
(?=.*[a-z])A lookahead: somewhere ahead there is a lowercase letter. It checks without moving, so the next lookahead starts from the beginning again.
(?=.*[A-Z])Somewhere there is an uppercase letter.
(?=.*[0-9])Somewhere there is a digit.
(?=.*[^A-Za-z0-9])Somewhere there is a character that is not an ASCII letter or digit: a symbol, a space or a letter such as é.
.{8,}Eight or more characters of any kind except line breaks. This is the part that actually consumes the password.
$End of the string.

What it matches and what it rejects

InputResultWhy
Passw0rd!matchesPasses every rule, and is still one of the first guesses any attacker tries.
correct-Horse-7matchesHyphens count as symbols.
Tr0ub4dor&3matchesPasses.
passwordrejectedNo uppercase letter, digit or symbol.
Password1rejectedNo symbol.
PASSWORD1!rejectedNo lowercase letter.
Pa1!rejectedShorter than 8 characters.
correct horse battery staplerejectedA long passphrase, rejected for having no capital or digit.

What current guidance says

The US National Institute of Standards and Technology revised its digital identity guidelines in 2025. In SP 800-63B-4, section 3.1.1.2, verifiers:

  • shall require at least 15 characters for a password used on its own, or 8 when it is one part of multi-factor authentication;
  • should allow at least 64 characters;
  • shall not impose other composition rules, "e.g., requiring mixtures of different character types";
  • shall check new passwords against a blocklist of common, expected or compromised passwords;
  • shall not force periodic changes, only a change when there is evidence of compromise.

Composition rules push people towards predictable patterns like Passw0rd!, which the regex above accepts, while turning away long passphrases. If you can choose, check length with the pattern below and compare against a blocklist:

/^.{15,64}$/u
InputResultWhy
correct horse battery staplematches28 characters.
fifteen chars!!matchesExactly 15.
Passw0rd!rejectedToo short, however many symbols it has.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrejected65 characters: over the limit chosen here.

The u flag makes JavaScript count characters rather than UTF-16 units, so an emoji counts as one character, not two.

If you have to enforce character rules

Company policy sometimes requires the classic rules. Even then, one regex is a poor way to enforce them: it can only say "no", not which rule failed. Test each rule separately and tell the user what is missing:

const RULES = [
  [(pw) => pw.length >= 8, 'at least 8 characters'],
  [(pw) => /[a-z]/.test(pw), 'a lowercase letter'],
  [(pw) => /[A-Z]/.test(pw), 'an uppercase letter'],
  [(pw) => /[0-9]/.test(pw), 'a digit'],
  [(pw) => /[^A-Za-z0-9]/.test(pw), 'a symbol'],
];
const missing = (pw) => RULES.filter(([ok]) => !ok(pw)).map(([, msg]) => msg);

console.log(missing('password1').join(', '));  // → an uppercase letter, a symbol
console.log(missing('Passw0rd!').length);      // → 0

The same applies on the server. Keep the rules identical on both sides, because the browser check is for the user's convenience and the server check is the one that counts.

Length, Unicode and the maximum

JavaScript's length and the . in a pattern without the u flag count UTF-16 code units, so an emoji counts twice:

console.log('😀'.length, [...'😀'].length);     // → 2 1   length counts UTF-16 units, not characters

Python counts code points and .NET counts UTF-16 units, so the same password can have different lengths in your front end and back end. Allow spaces and any Unicode character; there is no reason to forbid them. Set a generous maximum so nobody can send a megabyte to your hashing function, but well above what people type. Note that the classic pattern's . does not match a line break, so a password pasted with a trailing newline is rejected rather than silently changed.

In JavaScript, Python, PHP and C#

JavaScript

const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/;
console.log(re.test('Passw0rd!')); // → true
console.log(re.test('password')); // → false

Python

import re

pattern = re.compile(r'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}')
print(bool(pattern.fullmatch('Passw0rd!')))  # → True
print(bool(pattern.fullmatch('password')))  # → False

PHP

<?php
$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/D';
echo preg_match($pattern, 'Passw0rd!'), "\n"; // → 1
echo preg_match($pattern, 'password'), "\n"; // → 0

C#

using System;
using System.Text.RegularExpressions;

var pattern = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}\z");
Console.WriteLine(pattern.IsMatch("Passw0rd!")); // → True
Console.WriteLine(pattern.IsMatch("password")); // → False

Each version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.

The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.

Questions

What regex requires one uppercase letter, one lowercase letter, one number and one special character?

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$. Each lookahead checks one rule and .{8,} sets the minimum length. Change the 8 to raise it.

Are password complexity rules still recommended?

Not by NIST. SP 800-63B-4 says verifiers shall not impose composition rules such as mixtures of character types, and should instead require a minimum length and check passwords against a blocklist.

What counts as a special character in this pattern?

Anything that is not an ASCII letter or digit: punctuation, spaces, and letters outside A to Z such as é or ß.