Skip to content

matchRegex β€” GTM Variable Template for String

VARIABLES β€Ί STRING
matchRegex CORE String
Direct (.tpl) Apply (.tpl)

Matches a string against a regular expression pattern. Returns an array with the match and any captured groups, or null if no match found.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Extraction

Pull specific values, segments, or patterns from complex data structures.

Comparison

Test equality, containment, and ordering between values.

Regex

Pattern matching, extraction, and replacement using regular expressions.


Simple digit match
INPUT
String to Search: Hello world 123
Regular Expression Pattern: \\d+
OUTPUT
123
Email capture groups
INPUT
String to Search: Email: [email protected]
Regular Expression Pattern: (\\w+)@(\\w+\\.\\w+)
No match returns null
INPUT
String to Search: Hello world
Regular Expression Pattern: \\d+
OUTPUT
null

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

matchRegex
String to Search
πŸ’Ύ The string to search within.

Supported formats:
  βœ“ String
Regular Expression Pattern
πŸ” The regular expression pattern to match in the string.

Supported formats:
  βœ“ String (regex pattern): "^abc", "d+", "(w+)"

⚠️ Escaping note: In this UI field, use single backslash (e.g., d+ for digits). GTM automatically handles the escaping.
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the string before matching (e.g., normalize case, trim whitespace).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result array before returning it (e.g., arr => arr[0] to get full match only, arr => arr[1] to get first captured group, arr => arr !== null for boolean). Useful for chaining transformations on the output.
String to Search string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Regular Expression Pattern string
matchRegex()


πŸ“œ View Implementation Code
/**
* Matches the provided string against the given regular expression pattern.
*
* @param {string} data.src - The string to search within.
* @param {string} data.ptn - The regular expression pattern to match in the string.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before matching.
*
* @returns {Array|null} An array containing the matches if found, or null if no match is found.
*
* @framework ggLowCodeGTMKit
*/
const matchRegex = function(stringToMatch, regexPattern) {
if (typeof stringToMatch !== 'string' || typeof regexPattern !== 'string') {
return null;
}
return stringToMatch.match(regexPattern);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// matchRegex - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedString = applyCast(data.pre, data.src);
return out(matchRegex(processedString, data.ptn));
// ===============================================================================
// matchRegex(...) – Apply Mode
// ===============================================================================
/*
return function(stringToMatch, regexPattern) {
return out(matchRegex(stringToMatch, data.ptn));
};
*/
πŸ§ͺ View Test Scenarios (10 tests)
βœ… '[example] Simple digit match'
βœ… '[example] Email capture groups'
βœ… Test matching word at start of string
βœ… Test matching word at end of string
βœ… Test with special characters
βœ… Test with whitespace pattern
βœ… Test with non-string input returns null
βœ… Test with null string returns null
βœ… Test with empty pattern
βœ… '[example] No match returns null'