Password validation

2 snippets in Regular Expressions

RXRegular Expressions

Combining Lookaheads & Lookbehinds

RX · Lookahead & Lookbehind
syntax
(?<=prefix)pattern(?=suffix)
example
JS:  'price: [42] and size: [XL]'.match(/(?<=\[)\d+(?=\])/g)
Py:  re.findall(r'(?<=\[)\w+(?=\])', '[hello] and [world]')
output
JS: ["42"]  (digits inside square brackets, without brackets)
Py: ["hello", "world"]

Note You can combine multiple lookarounds on the same position. A common pattern for password validation stacks multiple lookaheads: (?=.*[A-Z])(?=.*\d)(?=.*[@#$]).{8,} checks for uppercase, digit, and special character all in one pass.

Stacked Lookaheads for Validation

RX · Lookahead & Lookbehind
syntax
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%]).{8,}$
example
JS:  const strong = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%]).{8,}$/
     strong.test('MyP@ss1word')   // true
     strong.test('weakpass')       // false
Py:  pattern = r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%]).{8,}$'
     bool(re.match(pattern, 'MyP@ss1word'))  # True
output
true / True for 'MyP@ss1word', false / False for 'weakpass'

Note Each lookahead checks a different requirement without advancing the match position. All must pass before .{8,} consumes the string. This is a widely-used pattern but be cautious about catastrophic backtracking with very long inputs -- consider checking each requirement separately in code for production validation.