Skip to content
Regex for Password Strength — Pattern Explained with Examples

Regex for Password Strength — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 3 min read

This regex validates password strength by enforcing multiple character class requirements using lookahead assertions. It ensures the password contains at least one lowercase letter, one uppercase letter, one digit, one special character, and is at least 8 characters long. This pattern is a standard baseline for security-conscious authentication systems.

The Pattern

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/

Pattern Breakdown

PartMeaning
^Start of string
(?=.*[a-z])Lookahead: at least one lowercase letter
(?=.*[A-Z])Lookahead: at least one uppercase letter
(?=.*\d)Lookahead: at least one digit
(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?])Lookahead: at least one special character
.{8,}At least 8 characters (any character)
$End of string

Matches

  • Password1!
  • Str0ng#Pass
  • MyP@ssw0rd
  • 1Secure$Pass
  • Abcd1234!
  • P@ssw0rd!

Does NOT Match

  • weak — too short and no uppercase, digit, or special char
  • password — all lowercase, no variety
  • ALLCAPS123 — no lowercase or special char
  • lowercase1! — no uppercase
  • NoDigits!! — no digit
  • Short1! — only 7 characters
  • abcdefgh — no uppercase, digit, or special char

Language Examples

JavaScript

const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;

console.log(passwordRegex.test('Password1!'));   // true
console.log(passwordRegex.test('weak'));          // false
console.log(passwordRegex.test('SHORT1!a'));      // true (8 chars)

// Provide user feedback
function checkPasswordStrength(password) {
  const checks = {
    length: password.length >= 8,
    lowercase: /[a-z]/.test(password),
    uppercase: /[A-Z]/.test(password),
    digit: /\d/.test(password),
    special: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password),
  };
  return checks;
}

Python

import re

password_regex = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};:\'"\\|,.<>\/?]).{8,}$'

print(bool(re.match(password_regex, 'Password1!')))   # True
print(bool(re.match(password_regex, 'weak')))          # False

# Provide user feedback
def check_password_strength(password):
    return {
        'length': len(password) >= 8,
        'lowercase': bool(re.search(r'[a-z]', password)),
        'uppercase': bool(re.search(r'[A-Z]', password)),
        'digit': bool(re.search(r'\d', password)),
        'special': bool(re.search(r'[!@#$%^&*()_+\-=\[\]{};:\'"\\|,.<>\/?]', password)),
    }

print(check_password_strength('Password1!'))

Common Pitfalls

  1. Lookaheads don’t consume characters — Each (?=...) checks for a condition without consuming input. The final .{8,} is what actually matches the content. Without it, the lookaheads would validate but match zero characters.

  2. Special character definition varies — Different systems include different sets of special characters. The set [!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?] is common but may need adjustment. Avoid including characters that cause issues in URL encoding or storage.

  3. Length minimum — 8 characters is the industry minimum (NIST SP 800-63). Consider requiring 12 or more for high-security applications. Note that the {8,} only checks character count, not complexity distribution.

  4. Common passwords pass regex but are weakPassword1! passes this regex but is a commonly used, easily guessed password. Combine regex validation with a password dictionary check for better security.

Real-World Use Cases

  • User registration — Enforcing minimum password complexity requirements during sign-up
  • Password change flows — Validating new passwords meet security policies in account settings
  • Admin user management — Enforcing organizational security policies for employee accounts

FAQ

Yes. Display a checklist of requirements (uppercase, lowercase, digit, special char, 8+ chars) and update it in real-time as the user types. This improves UX and reduces registration friction.
No single regex is sufficient. Combine it with: password length limits (max 64–128), a common-password blacklist, rate limiting, bcrypt/argon2 hashing, and multi-factor authentication. Never store passwords in plain text or using reversible encryption.

Related Patterns

Previous Regex for Hex Color Codes — Pattern Explained with Examples Next Regex for Username/Slug — Pattern Explained with Examples

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro

Home Browse Regex Pattern Library