
May 8, 2025
Regular Expressions in Google Tag Manager and Google Analytics 4
Updated: May 8th, 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 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.

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 “\” character is used when you want to interpret the proceeding character literally and not as a special regex character.
For example, “/shoes/search\?q=.*” matches “/shoes/search?q=sneakers”. The “?” is not used to match the previous character 0 or 1 times, but rather as the literal character.
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.
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)** ?