Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lookaround

Assertions that match a position without consuming characters.

PatternNameMeaning
(?=...)Positive lookaheadFollowed by …
(?!...)Negative lookaheadNOT followed by …
(?<=...)Positive lookbehindPreceded by …
(?<!...)Negative lookbehindNOT preceded by …
;; Match a number followed by "px" (but don't capture "px")
(bind rx (regex "\\d+(?=px)"))
(bind text "width: 16px")
(bind m (rx:exec text))
(console:log (get m 0))    ;; → "16"

;; Match digits NOT preceded by "$"
(bind rx2 (regex "(?<!\\$)\\d+" "g"))
(bind text2 "price $42 count 7")
(for-of m (text2:match-all rx2)
  (console:log (get m 0)))  ;; → "2", "7"  (not "42")

Lookaround is powerful for extracting values from context without including the context in the match. Use lookahead when you care about what follows; lookbehind when you care about what precedes.