How to use RegEx for Google Tag Manager – The Ultimate Guide

Regular Expressions (ReGex) are a powerful tool within Google Tag Manager to create more versatile and precise tracking deployments. You can save a lot of time and avoid unnecessary duplication of tags, triggers, and variables by using them.

Therefore, I think they are an essential tool when it comes to leveling up your implementation skills. But RegEx can be so much more. Nowadays RegEx can be used in Google Analytics, Data Studio, and Google Sheets as well as in Google Tag Manager. 

But for now, let’s learn some great ReGex for Google Tag Manager techniques.

What Are Regular Expressions?

CTRL + F – What does this stand for? You guessed right: Search. It’s a universal tool for anyone working with computers nowadays, as we are dealing with an increasing amount of data every day and we need a way to find what we are looking for. 

When you type your query into the box that comes up. How does the computer match your query to the given information? Should it care about the capitalization of letters? Or ignore it? Only look in the title or also in the body of text? For sure you have seen all kinds of ways to narrow down your search in forms and today you’ll discover the most complex, but powerful tool of it all: Regular Expressions.

In short, they are simply another way to tell your program HOW to search through the context. In Google Tag Manager they are often used in the Filter options of a Trigger with the Matches RegEx option (but there are more ways to use them, as we will soon discover).

An Example of RegEx in Action

I use RegEx in all kinds of places within Google Tag Manager, but probably my most often used RegEx includes this little pipe symbol | Why? Because it lets me express an OR within one expression. Let’s take this example:

Imagine you have two triggers. One that fires your tag on the Button Click of Add To Cart and one to enter the Checkout.


You could, of course, keep both triggers, attach them to your tag (or tags) and be all good. But if you want to be truly efficient, you could build a new trigger that encompasses both button clicks. It would look like this:


So what does this do? Let’s attach this to a tag and try it out in the Preview mode. We click on one of our buttons, see our tag fire and click on it (in the Preview mode). Now we can inspect why our Trigger turned true. And we see these results:

single-trigger-fired-on-both-button-clicks-using -regex

What just happened? Well through the magic of the Matches RegEx options we were able to match both button click texts and save us the setup of another trigger. Neat, right?

How Can You Write ReGex for Google Tag Manager specifically?

ReGex in Google Tag Manager is a bit like programming. You can get basic things done like an OR operation, but you can also go super complex. To be honest, I haven’t figured out all the intricacies of Regular Expressions myself, but Google and StackOverflow are great resources when it comes to cases where RegEx should be able to help out. So don’t be afraid to do a bit of digging online.

Like any (programming) language, there are two things to master when learning to write Regular Expressions: Syntax and Semantics. Syntax describes the grammar you need to know to form a valid RegEx. Semantics is about the meaning like OR is expressed with a |.

Some important RegEx principles to keep in mind:

  1. You can use RegEx to extract, validate or replace information within a string of text. In most cases within Google Tag Manager you would use RegEx for validation or matching.
  2. By default RegEx can be seen to be similar to the CONTAINS option, that changes once you use special characters that have meaning
  3. There are different flavors of RegEx. Google Tag Manager is based on JavaScript, so that’s the flavor that will be used. Your particular RegEx string might not work in PHP, Python or other programming languages

ReGex Cheat Sheet for Google Tag Manager

Let’s have a look at all these special characters I’ve mentioned. This Cheat Sheet was made with Google Tag Manager use cases in mind. Therefore it doesn’t feature all special characters, but the ones that I deemed most important. If you are looking for a complete list, you might want to check out a very in-depth site about all things Regular Expressions.

How to Learn ReGex for Google Tag Manager?

By now you hopefully know what RegEx is all about and know a bit about the Syntax. But how can you actually use it? I know it would be tempting to try it out in Google Tag Manager directly, but I fear you will get into a loop of entering a RegEx, switching to the Preview mode and finding your Tag not firing. Then you make a few tweaks and try again…. This is a noble approach but it might leave you feeling frustrated. So I recommend you try whatever RegEx you want to use on this great site first,


Here is how you can use RegEx101 in 3 simple steps:

  1. Enter your Test Data

In the big window in the middle, enter your test string. What would that be? For example a bunch of URLs like these:

  1. Enter your RegEx

The big input field on the top is where your RegEx goes. Here is where you can experiment and see in real time, what your RegEx matches. Let’s enter .* as a test

  1. See what matches

You should see your Test Data entries light up blue. What does this mean? Dot Star in RegEx means: Match any character zero or more times. So in this case it matched everything.


Caution! As mentioned before, Regular Expressions can have different flavors. You might need to make sure your Engine is set to JavaScript and the flag (the part after the / of your RegEx) is set to g (for global).

Now let’s try out some more Regular Expressions. Here are some challenges:

  1. Match all the URLs that contain
  2. Match only https URLs
  3. Match only the URLs that end in -2/
  4. Match any numbers in the URL

(see the solution all the way at the bottom of this post.)

That wasn’t too hard, right?

Where Can You Use ReGex in Google Tag Manager?

Now that you know how to test your RegEx, it’s time to dive into Google Tag Manager and look at all the different places you can use it.


You can utilize Regular Expressions in different Places in Triggers. The first one being your custom Event Trigger. Here you have a field where you can activate RegEx.


This could be used to match up different custom events, that should all fire the same tag. I have used it before when I installed Enhanced Ecommerce Tracking with the GTM4WP plugin (more on that later).

The second (and more common) place where you can utilize RegEx is in your filter settings. Here you have 4 Matching options:

Matches RegEx

Matches RegEx (Ignore Case)

Does not match RegEx 

Does not match RegEx (Ignore Case)


What is the Ignore Case all about? As mentioned RegEx comes in different flavors. The Ignore Case is a special flag (you can activate it in RegEx101 as well) to ignore the Capitalization of Characters. When used, your whole RegEx will not be case sensitive.

With this:

Facebook and FaceBook would be matched by the RegEx: facebook



Regular Expressions can be even more powerful when used in Variables. Why? As you might know, variables are an underlying component in Triggers. Therefore you could use RegEx in your variables and then again in your Triggers (if you wanted).

The RegEx Table Variable


Like the normal Lookup Table Variable, with this variable you will be able to turn an Input Variable into your desired output. It’s a bit like an IFTT (if this then that) variable. But with the RegEx Table you can of course use a Pattern as a matching option. 

Let’s say your input variable is your {{utm_source}}. You often find your team writing Facebook, facebook, faceBook or FaceBook into this field. Annoying for later source reporting.

With the RegEx Table you can easily write the Pattern f?F?aceb?B?ook and output facebook to standardize your utm source.


The RegEx table is governed by some configurations that are hidden in the Advanced Settings tab.


Ignore Case – Like in the trigger option, this will ignore any capitalization of letters (yes, this renders our above pattern pretty useless, but it’s just an example.

Full Matches Only – This option renders our default RegEx of “contains” useless. You need to have a full match with your pattern in order for your Output to trigger. You could also think of it as GTM adding a ^ at the beginning and $ at the end of your RegEx.

Enable Capture Groups and Replace Functionality – This is a powerful one. You can capture parts of your Input variable and then output them into your Output value by using the dollar-replacement syntax. For example you could capture something in your pattern with a capture group ( ) and then output it by typing $1.

Custom JavaScript Variable

Another prominent place where you can utilize your Regular Expression skills is the custom JavaScript Variable. Regular Expressions are a part of JavaScript just like they are a part of any other programming language.

To initiate a regEx you can use the literal (you need the two / around your regEx) or the constructor function.

Var regex = /measures?S?cho+l/g  // literal

Var regex = new RegExp('measures?S?cho+l')// constructor function

Once you have your ReGex initiated, you can use it on any string with different JavaScript methods

var regex = /measures?S?cho+l/g  // literal

var myString = “measureschool”

myString.exec(regex) // result: measureschool

myString.test(regex) // result: true

myString.match(regex) // result: [measureschool] // result: 0

myString.replace(regex, "new") // result: new

myString.split(regex) // result: ["", ""]

A quick description of the different methods:

exec()This executes your pattern and returns whatever was matched in an array. If nothing is matched then null will be returned.
test()If you only want to know if there was a match with your RegEx, then you can achieve just that with the test() method. It will return true or false in the end.
match()Similar to the exec() method this matches your pattern and outputs your matches in an array.
search()Just like the test() method, but this outputs the index of your match, or a -1 if nothing is found. Particularly interesting if you search through and array.
replace()This takes to parameters: Your RegEx and the replacement. The method then matches your pattern and replaces any match in your string with your second parameter.
split()If you are looking to split up a string in two or more pieces, then this is the method you want to use. Enter the regEx and everything that is found before and after will be put into an array.

If you want to find out more about these methods check out this post on the Mozilla Developer Network. A great resource for anything JavaScript.

How Can You Use RegEx in a Custom JavaScript Variable

Now that you know how you can utilize RegEx in JavaScript – How do you use it in a custom JavaScript variable?

There are of course many ways, as the custom JavaScript is one of the most versatile variables we have in GTM.

Here is an example on how to extract the domain from a Click URL (Caveat: This assumes your URL end ins .com)


var regex = /https?:\/\/(.*\.(com))\//

return regex.exec({{Click URL}})[1]


A quick walkthrough of the code: Let’s start on line 2 where I save our RegEx in a  Variable. Here I’m using the literal notation. Of course I’ve tested this previously inside of RegEx101, so this should be ok. On line 3 I use our previously saved ragex and execute it with the exec method. As and argument I fill in our Click URL GTM Variable. The way exec works it will return an array of results. I want to return our first capture group and therefore use the [1] to return the second part of the array.


Let’s try this out. A prerequisite is of course a Click Trigger and activated Auto-Event Variable. In our Preview mode we can see that our new Variable (here called Click Domain) pulled out our domain from the Click Url variable.


Besides Variables and Triggers you can also use RegEx in Tags. The use case here is limited to Custom HTML tags, where you can also use Regular Expressions in the context of JavaScript. Thus it’s the same as in custom JavaScript variables and we won’t repeat the examples from above.

Examples of using RegEx in Google Tag Manager

Combining Triggers

Simple triggers can often be combined into one, with the help of Regular Expressions. This keeps your Containers clean, organized and small – a best practice in Google Tag Manager. 

Example: Let’s say you have two triggers attached to a tag. These triggers determine to fire your tag on the pageview of /path1 and /path2. While two triggers are totally ok to use, let’s try to optimize this through a Regular Expression.

RegEx: \/path1|\/path2

With this Regular Expression we can combine our triggers into one, with a new condition using RegEx.


Matching Empty Strings in Triggers

Use case: If you see (not set) in your Analytics tool (like Google Analytics) in your events it might be that the value you tracked is empty. You might have asked yourself: Why track this at all? Instead of filtering it inside your tool like Google Analytics, we can skip the measurement of it entirely, by using Regular Expressions.

RegEx: ^$

This RegEx matches empty values, as the caret and dollar sign would match any values that start and end with nothing. You could use it as a negative trigger condition to filter out any cases where the variable would stay empty. 


Match Everything with RegEx

Do you want to fire your tag on every page? Yes, there is a trigger for that. The All Pages trigger matches any URL and it’s built into Google Tag Manager by default. But what if you wanted to recreate it or e.g. build an All Pages trigger that runs on Dom Ready? Well, just use this simple RegEx.

RegEx: .*

The Dot Star pattern matches every string and all combinations that it is applied to. Really great if you create a trigger that you want to fire all the time. For example a Dom Ready – All Pages trigger


Building a Custom Event Trigger for GTM4WP Events

If you are using the GTM4WP Plugin to implement Enhanced Ecommerce Tracking onto your WooCommerce shop then you see all kinds of different events that are custom in your data layer.


You can use these custom events to build custom event triggers, but instead of building multiple triggers, why not combine them? In the custom event trigger we have the ability to use RegEx as well.

RegEx: gtm4wp.addProductToCartEEC|gtm4wp.productClickEEC|gtm4wp.removeFromCartEEC|gtm4wp.checkoutOptionEEC|gtm4wp.changeDetailViewEEC|gtm4wp.orderCompletedEEC


If you want to find out the other steps on how to install Enhanced Ecomemrce tracking for the GTM4WP Plugin checkout this guide.

Trigger: File Download Trigger

If you have direct downloads to files you want to track, then this click trigger can pick up any clicks on files with your preferred file extension.

RegEx: .(png|docx|jpg|pdf)$

This RegEx uses a capture group and the pipe | for an OR operator to match any URL that ends in these file extensions. The dollar sign makes sure it’s at the end of the URL.

You can attach this rule in combination with a click trigger.


Individual Scroll Depth Percentages on Certain Pages

A Scroll Depth trigger can be quite resource-intensive (many hits are sent to GA) and might lead to a lot of noise if you deploy it on every page. Luckily, GTM gives you the ability to only trigger your tags on a certain percentage of the page. But what if you wanted to measure more frequently on your Landing Pages and only twice on your blog pages? Then you can use a RegEx table in combination with your Scroll Depth trigger:

RegEx: /page/


Implement this into your Scroll Depth Trigger.


Variable: Extracting First Folder Path from URL

This RegEx Table variable will return the first  folder name in your URL. This might be something you want to use for Content Grouping, a trigger condition or maybe feeding it into your tracking tools. 

RegEx: .*\.com\/([a-zA-Z\d-]*)\/.*

This RegEx will look for a /shop/  

The RegEx assumes your URL path start after .com (but you can also switch it or use a | to enter multiple domain endings). It looks for the part between the forward slashes / and picks up any character including digits and the minus sign.


Bonus: If you wanted to get the second, third or n-th page path, just add another .*\/ in front of the capture group.

Variable: Normalizing Telephone Numbers

This variable is straight out of the playbook from Simo Ahava. It normalizes Telephone numbers in a RegEx Table and outputs a clean format. I could imagine using this if I wanted to send a clean format from a form to a tracking tool, without having to re-edit all the different formats of phone numbers a user might use in the form

Regex: 0(\d+))

This Regex uses the \d character which stands for digits between 0 and 9. The Plus + character is a quantifier matching repeating character at least once or more. Perfect for this kind of task. The lookup table would look:


As you can see this uses the capture groups mentioned earlier.

Variable: Remove PII

This variable will detect PII in your URL and remove it. This might be something that you want to do in order to prevent personal identifiable information entering your Google Analytics account.

RegEx: (.*)(.{4}@.{4}\.)(.*)

This RegEx will pick match email addresses in a URL string. To use it you will need to create a Regex Table:


Ensure the Input Variable is the {{Page URL}} and you have the three advanced options ticked.

remove-personal -information-from-google-analytics-with-regex

Rewriting Inputs

There are many ways to get to an outcome in Google Tag Manager. While a lot of examples exist on the internet on how to use the custom JavaScript variable to rewrite a variable input, let’s use our RegEx table to do the same.

As an example: Let’s say you have captured the price $4,99 and you want to turn it into 4.99 (without the $ sign and a dot instead of a comma). We can accomplish this with Regular Expressions.

RegEx: ^\$(\d+),(\d+)

Output: $1.$2


The RegEx will match everything after the $ sign and look for digits before and after the comma. Those will be put in groups that then will be output in the variable.



In this (rather extensive) guide we have discovered what Regular Expressions are, how to use them and given you some use cases to try in Google Tag Manager. As you can see, learning about RegEx can be a super valuable skill to learn.

You can use them to make more efficient triggers, pull and rewrite data in variables and even use them in Tags with custom JavaScript. It’s definitely one of my top skills to learn when trying to master Google Tag Manager.

If you followed along with our few challenges at the beginning of this post. Here are their solutions:

1. demoshop\.com
2. ^https.*
3. -2\/$
4. \d+

Remember, there are always multiple ways to solve problems with RegEx. If you came up with a different solution, but the same results: That’s great!

Are you fluent in RegEx already? If yes, you can probably think of more use cases than I have come up with here. If so, I’d love to hear from you in the comments below. 

And if you want to learn more advanced topics just like this then be sure to check out MeasureMasters.


Master Data & Analytics with Measuremasters

Exclusive Courses & Workshops | Ongoing Troubleshooting | Support Resources, Tools & much more
Newest Most Voted
Inline Feedbacks
View all comments
Mike Sacco
Mike Sacco
11 months ago

You did a post once on how to replace the phone number with the Tag Type “Google Ads Calls from Website Conversion”. Would I be able to use ReGex to include multiple phone numbers. 888.999.4567|888.999.5678|888.999.6789

11 months ago

Superb 🙂

11 months ago

This is an awesome guide, thank you Julian! Just as an FYI, it looks like there’s a typo in your title tag for this page.

10 months ago

Hi Jullian

You have mentioned removing PII through RegEx. With this solution, you are already hitting Google analytics server to collect PII, and it is too late to redact through RegEx. What you are doing here is simply deleting the PII, not redacting before sending hits to GA. Or, am I missing something?

8 months ago

Hi Julian, we have an issue on our website, we do not understand why, when we set up the click trigger, once we are going on the website, all the “clickclasses” or “clickID” strings are empty, and we cannot actually create triggers for specific parts or button of the webpage. Do you know why? it is maybe due to the woocommerce theme we are using?

Ryan Falcon-Hay
Ryan Falcon-Hay
5 months ago

Been looking for a simple guide to using RegEx and should have known Julian would have one! Thank you, your articles and videos are brilliant…been a subscriber since the GTM training days.

now it's time to

Start measuring like a master

Itching to jump into the world of MeasureMasters? This is what you have to look forward to.