Skip to content

matchRegex — GTM Variable Template for String

VARIABLES › STRING
matchRegex CORE String

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


When to Use This

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.


Examples

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

GTM Configuration

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()


Under the Hood

📜 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'