Advertisement

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.

The pattern
\((.*?)\)
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
(hello)Matcha lone pair matches, and group 1 holds helloTry →
price (in USD)Matchthe text inside the pair is what gets capturedTry →
()Matchthe lazy dot may be empty, so an empty pair still matchesTry →
no parens hereNo matchthere is no literal ( to start the matchTry →
(unterminatedNo matchan opening ( with no closing ) never matchesTry →
(a(b)c)Matchit matches, but only up to the FIRST ) — nesting is not trackedTry →
(line one line two)No matchthe dot will not cross the newline, so the pair is missedTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.