Lookarounds in JavaScript

From the JavaScript Regular Expressions cheat sheet · Groups & References · verified Jul 2026

Lookarounds

Assertions that don't consume characters

javascript
(?=x)   Positive lookahead
(?!x)   Negative lookahead
(?<=x)  Positive lookbehind
(?<!x)  Negative lookbehind

// Lookahead examples
/\d+(?=px)/.exec("100px")        // ["100"] (number before px)
/\d+(?!px)/.exec("100em")        // ["100"] (number not before px)

// Password validation with lookahead
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/
// Requires: lowercase, uppercase, digit, 8+ chars

// Lookbehind examples
/(?<=\$)\d+/.exec("$100")        // ["100"] (number after $)
/(?<!\$)\d+/.exec("€100")        // ["100"] (number not after $)

// Practical examples
"test@email.com".match(/\w+(?=@)/)      // ["test"]
"$1,234.56".match(/(?<=\$)[\d,]+/)      // ["1,234"]
"not.a.file.txt".match(/\w+(?=\.txt$)/) // ["file"]
💡 Lookarounds are zero-width assertions - don't capture text
⚡ Use multiple lookaheads for complex validation rules
📌 Lookbehind has limited browser support in older versions
🟢 Great for matching based on context without including it
lookaroundassertions

Continue with JavaScript Regular Expressions

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet