Digital Marketing Guide To Regular Expressions

Regular Expressions or “RegEx”, are patterns used to match character combinations in strings. In addition, Regular Expressions or “RegEx,” is a powerful criteria-based language that can help your data-driven tools such as Google Tag Manager. In JavaScript, regular expressions are also objects.


They help you to build complex logic in single trigger. You can combine multiple triggers and save more time by using RegEx. 


Nowadays, RegEx can be used in loads of different tools like Google Analytics, Google Data Studio, Google Sheets, and Google Tag Manager and etc. 


If you want to build patterns you have to learn the RegEx characters. These characters are mostly used in GTM.

 


RegEx #1 |  meaning  - OR


Example: a|b – matches a or b 


RegEx #2 .  (single dot) meaning is any single characters


Example: a.d – it matches ajc, acc, abc...


RegEx #3 ? - zero or one previous character


Example: goo?gle – matches gogle and google, but not gooogle


RegEx #4 *  - zero or more previous characters


Example: goo*gle – matches gogle, google, gooogle


RegEx #5 + one or more previous characters


Example: goo+gle – matches google, gooogle, but not gogle


RegEx #6 ^  start of the string    


Example: ^apple - matches "apple juice", but not pineapple


RegEx  #7 $  end of the sting    


Example: tag$ - matches pineapple, not apple juice


RegEx #8  []  list of items to match to


Example: [a-z] – matches any lowercase letter from a to z, h2[a-z] - matches h2h, h2o


RegEx #9 ()  group elements


Example: Man(ager) matches Man, Manager


RegEx #10  {} define character count {x}, {x,y}    


Example: [0-9]{2} – matches any two number string  from 01 to 99


RegEx #11: \ treat RegEx characters like normal characters


Example: \? - matches a question mark, not zero or one character

 

How you can use RegEx in Google Tag Manager?


Triggers with RegEx


You can utilize RegEx for Triggeres. For example, on Custom Event section you can easily activate RegEx matching. 

 

The most used (and more common) place RegEx is in your filter settings. Check all four matching options:

 

 

  • Matches RegEx
  • Matches RegEx (ignore case)
  • Does not match RegEx 
  • Does not match RegEx (ignore case)


RegEx with Variables


RegEx can be also used on RegEx Tables


RegEx Table is a kind of Lookup Table. However, it allows you to use a specific pattern as a matching option for variable. 

 

 


Ignore Case – This checkbox will ignore any capitalization of letters.
Full Matches Only – This option need to have a full match with your pattern in order for your Output to trigger. 
Enable Capture Groups and Replace Functionality – This one is really powerful one. You can capture parts of your Input variable and then output them into your Output value by using $ -replacement syntax. 


Custom JavaScript Variable


As a part of Custom Javascript, you can utilize it, like below

 


Most common RegEx examples in Google Tag Manager

Matching Empty Strings in Triggers - RegEx ^$


This one is used to define empty string in your conditions


Match Everything with RegEx - RegEx .*

 

 


This RegEx pattern means that match with everything. For example, you can use this RegEx as a condition of blocking triggers for different purposes.

As a result, you can use them to make more efficient triggers, get and rewrite data in variables, and even use them in Tags with custom JavaScript variable.