Lookaheads and LookbehindsLesson 3.4
Combining lookaheads and lookbehinds for complex conditions
chaining assertions, extract between delimiters, password strength pattern, assertion order, real-world extraction examples
Assertions Chain โ Order Matters
Combine lookahead and lookbehind at the same position to match text sandwiched between two delimiters without including the delimiters in the match.
// Extract content between [[ and ]]
const text = 'Use [[variable]] or [[name]] here';
text.match(/(?<=\[\[)[^\]]+(?=\]\])/g)
// => ['variable', 'name']
Strong Password Validator
function isStrongPassword(pw) {
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%]).{10,}$/.test(pw);
}
Each lookahead runs at position 0, independently checking a separate requirement.
