Between delimiters

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.

Non-Greedy Scanning Patterns

RX · Advanced Techniques
Syntax
.*?target  (scan forward minimally until target is found)
Example
JS:  'START data1 END noise START data2 END'.match(/START(.*?)END/g)
Py:  re.findall(r'START(.*?)END', 'START data1 END noise START data2 END')
Output
JS: ["START data1 END", "START data2 END"]
Py: [" data1 ", " data2 "]  (findall returns groups)

Note The .*? idiom scans forward character by character until the following pattern matches. This is the standard approach for extracting content between delimiters. Be aware that .*? can still cause slowdowns on long strings with no match -- consider using negated character classes instead when possible.

Frequently asked questions

How does Regular Expressions handle between delimiters?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "Combining Lookaheads & Lookbehinds" snippet in Regular Expressions uses `(?<=prefix)pattern(?=suffix)`.
Which pattern does the Regular Expressions example use?
The "Combining Lookaheads & Lookbehinds" snippet uses `(?<=prefix)pattern(?=suffix)`, from the Lookahead & Lookbehind section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "between delimiters"?
Besides "Combining Lookaheads & Lookbehinds", this page also shows "Non-Greedy Scanning Patterns".
Is there anything to watch out for?
Yes. For "Combining Lookaheads & Lookbehinds": 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.