How to Use REGEXP_REPLACE in BigQuery

Last updated August 23, 2026 · By the SaturnSQL team

REGEXP_REPLACE(value, pattern, replacement) rewrites every match. Use a raw r'' pattern, escape regex metacharacters, and write groups as \1.

SELECT REGEXP_REPLACE(phone, r'[^0-9]', '') AS digits_only
FROM users;

Escaping special characters

Characters like . * + ? ( ) [ ] { } | ^ $ and \ are regex syntax, so a literal match needs a backslash in front. Writing the pattern as a raw string (the r prefix) means you escape once for the regex rather than twice for the string literal. When the text to strip comes from a column or a parameter, plain REPLACE avoids the escaping question altogether.

-- Literal dots, not "any character"
SELECT REGEXP_REPLACE(version, r'\.', '-') AS dashed
FROM releases;

Reusing what you matched

Parenthesised groups are available in the replacement as \1, \2 and so on, which lets you reorder or reformat text in a single pass.

SELECT REGEXP_REPLACE('2026-08-23', r'(\d{4})-(\d{2})-(\d{2})', r'\3/\2/\1') AS eu_format;

REPLACE is faster when there is no pattern

REGEXP_REPLACE compiles a regex for every row. If you are swapping a fixed string for another fixed string, REPLACE does the same job with less work and no escaping rules.

SELECT REPLACE(url, 'http://', 'https://') AS secure_url
FROM pages;

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Do more with BigQuery

Related BigQuery guides