December 11, 2025
Regular Expressions in Google Tag Manager and Google Analytics 4
Updated: December 11th, 2025
Regular expressions are special sequences of characters used to find, extract, replace, or match text, making them powerful tools for text processing tasks in data analysis. In Google Analytics and GTM, it allows you to create flexible rules (such as matching multiple page URLs, excluding ranges of IP addresses, or grouping distinct campaign parameters) without creating individual rules for each variation.
In the context of GTM and GA4, you can use regex for trigger conditions, Regex table variables, filtering data in reports, and more!
This article will cover the tools you need to build regular expressions, such as basic syntax and patterns, and give some examples of how you can use regex in GTM and GA4.
Table of Contents
Here’s what you will learn in this article
- What are regular expressions?
- Basic syntax and patterns (with examples)
- Hot tips
- Regular Expressions in Google Tag Manager
- Regular expressions in Google Analytics 4
- Final Thoughts
What are regular expressions?
Imagine you’re trying to find all the email addresses in a very long document. You could read through it line by line, but that would take forever! A Regular Expression (often shortened to RegEx or regexp) is like a super-powered search command. Instead of searching for exact text, you define a *pattern* that describes what you’re looking for.
For instance, you could tell RegEx to find “any combination of letters and numbers, followed by an ‘@’ symbol, followed by more letters, then a dot ‘.’, and finally a few more letters.”
This pattern would then quickly find all email addresses, no matter what the actual address is. It’s a sequence of characters that specifies a search pattern, primarily used for pattern matching with strings, or string matching (i.e., “find and replace” like operations).
You will most commonly see âRegex matchesâ and âRegex does not matchâ in GTM and GA4. âRegex matchâ means that you will only include data that matches the regex pattern, and âRegex does not matchâ means that you will include all data except what matches the regex pattern.
Letâs look at an example to help: you have a site that sells shoes and accessories. Each shoe has a details page (/shoes/123-112233). You want to look at the aggregate of page views on these details pages.
You could use the contains filter in GA4, but you canât quite get this to work since the pattern for these URLS is â/shoes/[3 digits]-[6 digits]â. You canât just put â/shoes/â since this will include too many pages (perhaps â/shoes/listâ exists), and using â/shoes/123â will restrict the data too much. So, what do you do? Use regex!
Before diving into this example, letâs review some basic syntax and patterns you should be familiar with.
Basic syntax and patterns (with examples)
First, we will look at the basic syntax and later I will show you where can you use regex in GTM and GA4.
Wildcards
Wildcards provide flexibility by allowing for variations or unknowns in the input. There are five key characters that you should be familiar with:
- Period (.): Matches any single character.
- E.g. â/sho.s/â matches â/shops/â or â/shoes/â or â/shows/â
- Question mark (?): Matches the previous character 0 or 1 times.
- E.g. â/shoes?/â matches â/shoes/â or â/shoe/â
- Plus (+): Matches the previous character or group of characters at least once.
- E.g. â/shoes+/â matches â/shoess/â or â/shoesss/â
- This will be more useful once we discuss groups.
- Asterisks (*): Matches the previous character or group of characters 0 or more times.
- E.g. â/shoes*/â matches â/shoe/â or â/shoesss/â
- One of the most valuable patterns you will use is the â.*â combination, representing 0 or more of any character. For example, instead of constantly typing out âhttps://www.website.com{regex pattern}â, you can replace âhttps://www.website.comâ with â.*â. This will be very useful in GA4 since the interface needs the data to match the regex to the pattern exactly.
- Same as the plus sign, this will be more useful once we discuss groups.
- Pipe (|): Used as an OR statement.
- E.g. â/shoes/123|/shoes/abc)â matches â/shoes/123â or â/shoes/abcâ, but does not match â/shoes/a1b2c3â
- Learn more about the âOR conditionâ in this article.
Important: different tools treat regex differently.
- GTM (and standard regex): Often acts as a “contains” match. If your pattern is /shoes/, it will match /shoes/ anywhere in the string.
- GA4: Often defaults to a “strict” or “full” match. If you use “matches regex” in GA4, the pattern must match the entire string.
Because of this, you might frequently need to add .* (dot asterisk) at the beginning and end of your GA4 patterns to mimic a “contains” match. For example, use .*/shoes/.* to match the URL anywhere in the page path.
Anchors
Anchors specify positions within text that a pattern must match, such as the beginning or end of the string.
- Caret (^): Matches when the proceeding characters are at the beginning of the string
- E.g. â^/shoes/â matches â/shoes/123-112233â but does not match â/yourwesbite.com/shoes/123-112233â
- Dollar sign ($): Matches when the string ends on the previous character(s)
- E.g. â/shoes/$â matches â/yourwebsite.com/shoes/â but does not match â/yourwebsite.com/shoes/123â
Escape
The backslash â\â is used to treat the character following it as a literal text character, rather than a special regex command.
For example, a question mark â?â usually means “optional” in regex. If you want to actually target a question mark in a URL (like a query parameter), you must “escape” it by placing a backslash in front: \?.
- Pattern: /shoes/search\?q=.*
- Matches: /shoes/search?q=sneakers
Groups
Groups act as containers that allow you to denote parts of your pattern – a word, phrase or collection of characters – as a single unit.
- Round brackets or Parentheses ( ): Matches the exact order of the characters in the brackets.
- E.g. â(/shoes/)â matches â/shoes/123-112233â or â/womens/shoes/â
- Square brackets [ ]: Matches any order of the characters in the brackets.
- E.g. â/shoes/[123]â matches â/shoes/1â or â/shoes/2â or â/shoes/3â
- Hyphen (-): Creates a range of characters within the square brackets to match in any order.
- Common use cases:
- [A-Z] is all uppercase alphabetical characters
- [a-z] is all lowercase alphabetical characters
- [0-9] is all whole digits
- [A-Za-z0-9] is all alphanumeric characters
- Note that [A-z] is not the same as [A-Za-z]
- E.g. â/shoes/[0-9]+â matches â/shoes/102â or â/shoes/77â
- Common use cases:
- Curly brackets { }: Defines how many times the characters within square brackets need to appear in the string. There are three ways to do this:
- {n}: Exactly n occurrences.
- E.g. â/shoes/[0-9]{1}â matches â/shoes/1â or â/shoes/7â
- {n,}: At least n occurrences.
- E.g. â/shoes/[0-9]{1,}â matches â/shoes/6â or â/shoes/12â or â/shoes/123456789â
- {n,m}: At least n occurrences, but no more than m occurrences.
- E.g. â/shoes/[0-9]{1-3}â matches â/shoes/0â or â/shoes/14â or â/shoes/123â
- {n}: Exactly n occurrences.
Hot tips
I have several random (but useful) regex tips. I could not come up where to put them, thatâs why this section is just called âhot tipsâ.
- Regular expressions are case-sensitive. This means that â/Shoes/â is not the same as â/shoes/â. You may see a variation of âMatches regex (ignore case)â available, in which case this would mean that â/Shoes/â = â/shoesâ. I would recommend using this option to avoid missing any data.
- Additionally, you can use â(?i)â in front of any strings where you want to avoid case sensitivity, e.g. â/(?i)shoes/â matches â/Shoes/â or â/shoes/â.
- Make use of brackets to simplify expressions. For example, if you want to match either â/shoes/â or â/boots/â rather than using â(^/shoes/$)|(^/boots/$)â you can combine these into â^/(shoes|boots)/$â.
- Test your regex using a site like regex101.com. One thing to note is that this site requires you to use the escape more than is necessary for GTM or GA4.

- GA4 regex is a âregex matchâ, not a âregex containsâ. This means that your regex statement needs to match the dimension values exactly. For example, say you want to create a regular expression for every page that contains â/shoes/â. You will get no or limited data if you just set the regex to â/shoes/â. You should use the expression â.*/shoes/.*â since there could be more in the URL before or after the â/shoe/â.
- Regex is always generous, meaning it may sometimes include data you did not intend to use. Opposite of the .*, you will also want to ensure that you are using the hard stop $. This will ensure that you avoid unexpectedly including extra data.
- E.g. You want to capture the URL âwww.site.com/shoesâ. If you just set the regex to â.*/shoesâ, then this will also include URLs like â…/shoes/123â. The correct pattern would be: â.*/shoes$â. However, in GA4, itâs the opposite, so there will be an assumed â$â at the end of the regex pattern.
So, letâs go back to that example from before. How would you create a regular expression to capture a URL in the format â/shoe/[3 digits]-[6 digits]â? Take a moment to try this outâŚ
The answer: â.*/shoe/[0-9]{3}-[0-9]{6}$â
- .* says that anything can come before this in the URL
- /shoe/ is the part of the URL that tells us we are looking at a shoe page
- [0-9]{3}-[0-9]{6} is the format of the product ID for shoes
- $ says that the URL will end after the product ID
Regular Expressions in Google Tag Manager
You can use regular expressions in specific cases across tags, triggers and variables in Google Tag Manager.
In GTM, you may see up to four different selections that include regular expressions: âmatches RegExâ, âmatches RegEx (ignore case)â, âdoes not match regexâ, or âdoes not match RegEx (ignore case)â. The âignore caseâ means that âShoesâ is the same as âshoesâ.
Trigger conditions
When creating a trigger, there is a section that says, âThis trigger fires onâ, allowing you to restrict the trigger to fire only on certain variable conditions, including custom JavaScript, Data Layer variables, built-in variables, etc.

Element Visibility Trigger
Letâs say there is an element on your site that is available on multiple pages and has the same ID across all instances. Using the Element visibility trigger, youâd be able to track any time a user views element.
However, depending on which page the user views the elements, you might want to have separate events with separate triggers. In this case, you can set âPage path matches RegExâ and put the pattern for your page.

Using the example from before, we can set the page path equal to â/shoe/[0-9]{3}-[0-9]{6}â. The page path captures the URI, the portion of the URL following the domain, so we donât need to include â.*â.
Click Trigger
Now, imagine that there is a button you want to track the usage of, but the button can have two different texts and do the same action (this may be a UI issue, but letâs ignore that for now). You can use matches RegEx to capture both click texts with the click trigger.

One thing to note is that if your site has different language options, the click text will likely change, so I think itâs better to use the Click URL. Check out this article on the âOR conditionâ to see more examples of this.
Variables: RegEx table & custom JavaScript
Let’s take a look at how you can use regular expressions in Google Tag Manager Regex table variable and Custom JS variable.
RegEx Table: Content Groups
Content groups allow you to categorize content in a way that makes sense to the context of your site and report on these groups in Google Analytics 4. You may have come across situations where you want to see how different sections of your site are performing compared to each other.
While you can filter by each page in a GA4 Explore report, you wouldnât see all the aggregates unless you export your page-level data into another tool and then do some filtering magic to group your pages to find more meaningful insights.
Here, you can use the RegEx table variable to organize different path groupings into content groups so you can view all this data at once.

Why use Regex here? Standard lookup tables require exact URLs. If your blog posts follow a structure like /blog/category/post-name, a standard table can’t group them all under “Blog”. A Regex Table allows you to use a pattern like ^/blog/.* to capture dynamic URLs and group them into a single reliable bucket.
Iâd recommend reading the content groups article to get all the details about this topic since I think itâs quite an interesting thing that you can add to bring more insights to your analytics.
For example, you might have a few product detail pages that you want to capture to compare if there is a difference in how users interact with products on your site. Select â{{Page Path}}â as your input variable, which means that the RegEx table will be comparing the patterns you input to this variable.

Now, add the patterns and outputs you want to see in GA4 reports.

Regex Table: Rename PDF files
Another use of the RegEx table is to rename existing variables. If you have set up PDF download tracking, you may not like the format of the data that the filename variable collects, so that you can reformat it.
The scenario is: The marketing team that creates your catalogues uses an alphanumeric naming system. You have received complaints that the GA4 reports are difficult to understand since some people are unfamiliar with this naming system.
For example, each spring catalogue follows the format âSPR-[six digits or alphabetical characters].pdfâ.

Now, your GA4 reports are easier to read! You can still keep the file_name variable as a parameter in the event and then also add this as a simplified_file_name, so you still have the granular data.
Custom JavaScript: Extract product ID from URL
In this example, we will use custom JavaScript to extract a part of your siteâs URL. Reviewing JavaScript for GTM before going through this example may be helpful.
You can use a few key methods with your regex pattern to extract information from your site.
- .exec(): Performs a search on a string to find the first matching pattern and returns the match or null (if nothing is found).
- .match(): Like .exec(), this function performs a search on a string but returns all matches or null (if nothing is found).
- .replace(): Looks for the first match and replaces it with the substring provided.
- .replaceAll(): Looks for all matches and replaces them with the substring provided.
- .search(): Looks for the first match and returns the index of the match or -1 if there is none.
- .split(): Uses a fixed string or a regular expression to break up a string into an array of substrings.
- .test(): Looks for a match and returns true or false.
In this example, we will extract the product ID from the URL. Here, the URL has the format âwww.yourwebsite.com/product/id-1234,â where the ID can have any number of digits. So, part of the URL we want to extract fits the pattern â/id-[0-9]+â.
You can find the code for this example below:
function() {
var url = window.location.href; // current page URL
var regexPattern = /id-[0-9]+/; // extract the id pattern enclosed by /
var id url.exec(regex);
return id
}
One thing to note is that JavaScript requires you to enclose your regex with a forward slash (/).
Tags: Custom HTML
The only use for regex in tags is if you are creating a tag using custom HTML. This will allow you to use regex with JavaScript, just like in the custom JavaScript variable. So, you can use the same methods listed above to manipulate the data you will collect in variables within the tag.
In my article on how to transfer UTM parameters from one page to another with GTM, the custom HTML script includes regex, so you can check that out to see regex in action!
Regular Expressions in Google Analytics 4
In GA4, you may see up to four different selections that include regular expressions: âmatches regexâ, âpartially matches regexâ, âdoes not match regexâ, or âdoes not partially match regexâ. In most cases, “partially matches regex” will not be available, so if you donât see this option, you should always assume it is the exact match.
The difference between matches regex and matches partial regex is that partial matches regex acts like a âregex containsâ rather than a âregex matchâ. Letâs use âwww.website.com/shoes/123-112233â as an example.
Here, you could set the partial regex match as â/shoes/[0-9]{3}-[0-9]{6}$â but with the âmatches regexâ we would need to add the dot wildcard (.*) in front of the pattern, like â.*/shoes/[0-9]{3}-[0-9]{6}$â since it needs to be an exact match for the entire URL.
Standard report
There are two ways to filter a standard report: (1) use the âAdd filterâ button or (2) customize the report.
âAdd filterâ button
Go to the reports tab, find the report that you are interested in and click the âAdd filterâ button. Choose the dimension you want to filter the report by and select one of âmatches regexâ, âpartially regexâ, âdoes not match regexâ or âdoes not partially match regexâ and enter the pattern.
In this case, we will filter the report only to include visitors from Canada or the USA. Select âCountryâ as the dimension, select âmatches regexâ, and enter the pattern âCanada|USAâ (or whatever other countries you may be interested in).

Customize report
When you use the filter to customize a report, the filter element looks the same as above. However, once you are on the report you want to customize, click the pencil icon in the top-right corner and select âAdd filterâ from the Customize report tab on the new page.

Maybe you want to exclude any data from visitors in the USA or Canada, in which case you can reuse the same dimension and pattern from the previous example but use the âdoes not match regexâ condition.

Learn more about all the ways you can customize a standard report here.
Exploration
When using the Exploration filter in GA4, you can filter your reports on dimensions and metrics. When you filter by a dimension, you have the option to filter using a regex pattern, which provides you with more freedom in how you view your data. The filter will use âmatches regexâ so the pattern will need to be an exact match.
In your exploration, you will find the Filters element at the bottom of the Settings tab. You can either click on the filter element and select a dimension or drag a dimension into the filter.

You will need to ensure that the dimension you want to filter on has been inputted into the report first.

Google Analytics 4 automatically tracks some events. If you have a report that looks at all events, it may be annoying having these auto-tracked events in there; you may just want to see the custom events you created.
Using the âdoes not match regexâ condition, add in the events that you want to exclude for the report, separated by the pipe character (|), e.g. âpage_view|scroll|user_engagement|session_start|clickâ.

As a reminder, a workaround to be able to mimic a âpartially matchesâ condition is to employ the â.*â to look for partial matches. So, if you wanted all pages that contain âwomensâ, then you can use the pattern â.*womens.*â with the âmatches regexâ condition.

If you want a more comprehensive understanding of Explorations in GA4, check out this article.
Segments and Audiences
Segments allow you to slice and dice your data retroactively into subsets you can use in GA4 Explorations. Audiences will also enable you to divide your data into subsets, but it cannot be done retroactively. You cannot use audiences in Explorations, but you can use them in comparisons within the standard report. Learn more about the differences here.
Both of these subsets can be defined by multiple conditions, which can be filtered using regular expressions.
Segments
To create a segment, go to Explore and create a new report or use an existing report. In the Exploration interface, click the plus sign (+) next to Segments and select âUser segmentâ (this is what we will use for the three examples below).

As with all the examples in this article, the segments you choose will depend on your business needs. These are just some examples to get you started and show you how to use regex.
- Excluding users in specific countries: âCountry IDâ does not match regex âUS|CAâ
- Users who have visited blog pages: âPage locationâ matches regex â.*/(blog|article|posts|news)/.*â
- Users who visited specific product pages: âPage locationâ matches regex â.*/product/[A-Za-z0-9-]+$â
Audiences
To create audiences, go to Admin > Data display > Audiences. Click âCreate a custom audienceâ. You can use the same examples as above to provide inspiration for what you would want to capture in an audience. I would recommend reviewing the Google Analytics 4 Audiences article.

Custom channel groups
Custom channel groups allow you to categorize traffic sources into your defined channel groups. This means you can decide the names of the channels and the rules that dictate how to group the traffic sources into those channels.
One use case is that you may want to group all social media channels together. In this case, you can take all the platforms you would consider social media and separate them with the pipe symbol (|). You can put this into rounded brackets and then add (?i) in front so that the pattern ignores the case.
The final result will look like â(?i)(facebook|instagram|linkedin)â.
When creating conditions for custom channel groups, you will also be given the options of âpartially matches regexâ (which we will use in this example) and âdoes not partially match regexâ. This means that you donât need to worry about using â.*â to capture the entire string.

Internal traffic filtering
If you want to avoid tracking your own traffic on your site, you will want to use internal traffic filtering. This has some nuances, so I recommend reading how to exclude internal traffic in Google Analytics 4.
Go to Admin > Data collection and modifications > Data streams. Select the data stream you want to modify, scroll down to Configure tag settings > Show more > Define internal traffic.
Here, you can indicate the IP addresses you do not want to track. If you have multiple IP addresses, then you can use regex.

Define unwanted referrals
When you see âReferralâ in your traffic data, it describes visitors who came to your site from another site without using a search engine. GA4 allows you to list specific domains that are not to be recognized as referral traffic but rather be labelled as direct traffic instead.
To reach this feature, go to Admin > Data collection and modifications > Data streams. Select the data stream you want to modify, scroll down to Configure tag settings > Show more > List unwanted referrals.
Now, you can define the referral domains you want to show as âdirectâ instead of âreferralâ in your GA4 reports.

If you want to exclude sub-domains, use the pattern â.*(paypal|example)\.comâ.
Create and modify events
Directly in the GA4 interface you can create and modify events, which is helpful when you want to create a version of an existing event without needing to go through Google Tag Manager or a developer.
To reach this feature, go to Admin > Data display > Events > Create events.
Example 1: Create a new event by combining two other events
You may have two events that basically do the same thing, but in some cases, you still want to be able to view them as separate events. In this case, you can create a new event that combines the two events together.
Here, I am combining the form_submission and generate_lead events under a new event called form_completed.

Example 2: Create a new event based on 2 page URLs
Another scenario is that you may have multiple pages shown to a visitor when they have successfully completed some action, like a âthank youâ page after a purchase or a âsuccessâ page after signing up for emails.
You can create an event that captures every time a user views one of these pages, called thank_you_page. The URL will vary based on your site, so tailor your regex pattern to match your siteâs specific structure. However, for illustration purposes, weâll use the following example: âhttps://.*(success|thank-you).*â.

Note that when you use the page_location parameter, the input must start with “http://” or “https://”.
Regular Expressions in Google Tag Manager & GA4: Final Thoughts
Hopefully, after reviewing this article, seeing the term âregex*â within Google Tag Manager and Google Analytics 4 is less confusing, and you can leverage this new tool to enhance your events and reports.
The most important thing is to understand the basic syntax and patterns. From there, you can create anything you can think of to fit your business needs, from tags to segments to report filtering.
If you have any other tips or tricks up your sleeve when it comes to regular expressions, leave it in the comments below!


1 COMMENT
Thank you Julius for this useful article đ. Is it correct that the RegEx flavor for GA4/GTM/LookerStudio which needs to be selected at regex101.com is **ECMAScript (JavaScript)** ?