Skip to the content

Password Regex

The usual password pattern: eight characters or more with a lowercase letter, a capital, a digit and a symbol. Test it, and see what it misses.

Try:

JavaScript syntax, run by your browser's own RegExp engine.

6 matches.

Matches

  1. 1Tr0ub4dor&3index 0
  2. 2Blauwe-Fiets-42index 12
  3. 3aB3$efghindex 28
  4. 4Password1!index 93
  5. 5Qwerty123!index 104
  6. 6Pässw0rdindex 115
Cheat sheet

Characters

.
Any character except a line break
\d \D
A digit 0-9, anything but a digit
\w \W
A letter a-z, a digit or an underscore; anything else
\s \S
White space, anything but white space
[abc] [^abc]
One of a, b, c; any character except those
[a-z0-9]
A character in one of the ranges
\p{L}
A letter in any script (needs the u flag)
\. \\ \/
A literal dot, backslash, slash

How many

a*
Zero or more
a+
One or more
a?
Zero or one
a{3}
Exactly three
a{2,5}
Two to five
a{2,}
Two or more
a*? a+? a??
The same, but as few as possible (lazy)

Positions

^
Start of the text, or of a line with the m flag
$
End of the text, or of a line with the m flag
\b
The edge of a word
\B
Anywhere that is not the edge of a word

Groups

(abc)
A group whose text is remembered
(?<name>abc)
The same, with a name
(?:abc)
A group that is not remembered
a|b
Either a or b
\1 \k<name>
The same text a group matched earlier

Looking around

a(?=b)
a, only when b follows
a(?!b)
a, only when b does not follow
(?<=b)a
a, only when b comes before it
(?<!b)a
a, only when b does not come before it

In the replacement

$1 $2
The text of group 1, group 2
$<name>
The text of a named group
$&
The whole match
$` $'
The text before the match, after the match
$$
A dollar sign
Share this tool:TwitterFacebookLinkedInWhatsAppEmail

The pattern, piece by piece

The rule many sign-up forms apply: at least eight characters, with a lowercase letter, a capital, a digit and something else among them.

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9\s]).{8,}$/gm
^The start of the line.
(?=.*[a-z])A look ahead from where the pattern stands: somewhere further on there is a lowercase letter. Looking ahead uses up no text, so the next piece starts from the same spot.
(?=.*[A-Z])Somewhere there is a capital.
(?=.*\d)Somewhere there is a digit.
(?=.*[^A-Za-z0-9\s])Somewhere there is a character that is not a letter, a digit or white space: a symbol, or a letter with an accent.
.{8,}Only now is text taken: eight or more characters of any kind.
$The end of the line.

What it gets right

These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.

Tr0ub4dor&3Match
Blauwe-Fiets-42Match
aB3$efghMatch
passwordNo matchLowercase letters only.
PASSWORD123!No matchNo lowercase letter.
NoDigitsHere!No matchNo digit.
NoSymbol123No matchNothing but letters and digits.
Ab1!xyzNo matchSeven characters.

Where it goes wrong

The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.

Password1!MatchMeets every rule, and is among the first things an attacker tries.
Qwerty123!MatchSo is a row of keys with the usual ending.
Pässw0rdMatchNo symbol in sight: the ä passes for one, because the pattern only knows the letters a to z.
correct horse battery stapleNo matchFour random words: long and hard to guess, but without a capital or a digit.
mzqwkdtrbynxfhplNo matchSixteen random letters take far more guesses than eight characters of any kind, but there is no capital, digit or symbol among them.

Before you rely on it

A pattern like this checks the ingredients, not the strength. People satisfy such rules in the same few ways: a capital in front, a digit and an exclamation mark at the end. That is how Password1! gets through.

Current guidance, such as NIST's SP 800-63B, advises against rules about kinds of characters. It asks for a minimum length and for a check against lists of passwords that have leaked. If the rule is not yours to change, this pattern does the job. If it is, test the length and look the password up.

White space is left out of the symbols on purpose. In a box with one password per line, the line break behind each line would otherwise pass for the symbol, and NoSymbol123 would match. A space is white space too, so it does not count as a symbol either.

The password generator makes random passwords that pass.

The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.

See all 12