Pattern library / Extract & Replace
Regex to Match Text Between Parentheses
Pulling out the text that sits between a pair of delimiters is one of the most common regex questions (regex between parentheses, extract text between parentheses regex, regex match text inside parentheses). The pattern has three parts: the escaped literal \(, a lazy .*?, and the escaped closer \). The question mark is the whole trick — it makes the match stop at the first closing parenthesis instead of running to the last one. Nothing here needs a lookahead, lookbehind or backreference, so the same pattern works unchanged in JavaScript, Python, GA4 and BigQuery.
How the pattern works
- Lazy beats greedy:
\((.*?)\)stops at the first), socall (a) and (b)yields(a). The greedy form\((.*)\)runs to the last one and returns(a) and (b)— usually not what you want. - There are no anchors, so this runs in find mode and returns every fragment. The inner text lives in capture group 1:
[...s.matchAll(/\((.*?)\)/g)].map(m => m[1])gives each piece without the parentheses around it. - The dot never matches a newline, so a pair that spans lines is invisible (see example 7). For multi-line content switch to
[\s\S]*?, which also stays RE2-safe, or turn on the dotAll flag (s) in JavaScript and Go. - Other delimiters keep the same shape: square brackets
\[(.*?)\], braces\{(.*?)\}, angle brackets<(.*?)>. Quoted values have their own pattern — see regex-extract-quoted-string.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
(hello) | Match | a lone pair matches, and group 1 holds hello | Try → |
price (in USD) | Match | the text inside the pair is what gets captured | Try → |
() | Match | the lazy dot may be empty, so an empty pair still matches | Try → |
no parens here | No match | there is no literal ( to start the match | Try → |
(unterminated | No match | an opening ( with no closing ) never matches | Try → |
(a(b)c) | Match | it matches, but only up to the FIRST ) — nesting is not tracked | Try → |
(line one
line two) | No match | the dot will not cross the newline, so the pair is missed | Try → |
Where to use it
JavaScript (grab every parenthesised value)
const s = 'total (USD), tax (12%)'; [...s.matchAll(/\((.*?)\)/g)].map(m => m[1]); // ['USD', '12%']
BigQuery SQL (RE2 — same pattern, no change)
SELECT REGEXP_EXTRACT('total (USD)', r'\((.*?)\)');
-- returns: USD
Python (findall / group)
import re re.findall(r'\((.*?)\)', 'total (USD), tax (12%)') # ['USD', '12%']
Common mistakes
- Nested parentheses are not tracked. On
(a(b)c)the pattern returnsa(b— it stops at the first closer, so the trailingcis lost. Regex cannot count nesting: if the data really nests, walk the depth with a tiny parser instead of a pattern. - Empty pairs match.
()passes because the lazy dot may match nothing, so a search can hand back an empty string and validation code silently accepts a blank value. Require at least one character with\((.+?)\). - Unescaped parentheses are a group, not a literal.
((.*?))contains no literal parenthesis at all, so it matches an empty string anywhere —/((.*?))/.test("abc")istruewhile the escaped pattern correctly returnsfalse.
FAQ
How do I extract text between parentheses in JavaScript?
Run the pattern in find mode and read capture group 1: [...s.matchAll(/\((.*?)\)/g)].map(m => m[1]) returns every inner value, or /\((.*?)\)/.exec(s)[1] for the first one only. The parentheses themselves stay out of the result.
Why does my regex capture everything up to the last closing parenthesis?
You wrote the greedy .*. On call (a) and (b) greedy \((.*)\) returns a) and (b, while lazy (.*?) returns a. Add the question mark to make the quantifier lazy.
Does this pattern work in GA4, BigQuery and Go (RE2)?
Yes — it is only a literal, a lazy quantifier and a group, all of which RE2 supports, with no lookahead, lookbehind or backreference. The tester runs the browser engine, so results match Node exactly; check RE2 vs PCRE before shipping any pattern to a Google tool.
Related patterns
Quoted String Regex — Match "..." and '...' · Extract Key=Value with Regex — Capture Values from Log Lines · HTML Tag Regex - Find & Validate Tags
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.