Skip to content

regexTableLookupAll — GTM Variable Template for Value

VARIABLES › VALUE
regexTableLookupAll CORE Value

Matches a value against a table of regex patterns and returns all matching results.


When to Use This

Value Utilities

Handle edge cases — defaults for undefined/null, type checks, coercion.

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

Multiple regex matches
INPUT
Input String: /products/category/shoes
Regex Pattern Table: [
{rex: '/products/', val: 'Products Section'},
{rex: '/category/', val: 'Category Page'},
{rex: '/shoes', val: 'Shoes'},
{rex: '/admin/', val: 'Admin'}
]
OUTPUT
3
Single regex match
INPUT
Input String: /home/
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'},
{rex: '/about/', val: 'About'}
]
OUTPUT
1

GTM Configuration

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

regexTableLookupAll
Input String
💾 The input string to match against regex patterns.

Supported formats:
  ✓ String
Regex Pattern Table
📋 Table of regex patterns and their corresponding return values. All matching patterns will be returned in an array.

⚠️ Escaping note: In the regex pattern column, use single backslash (e.g., d+ for digits). GTM automatically handles the escaping.

Example:
  • Pattern: Hello → Value: greeting
  • Pattern: d+ → Value: number

*** Multiple regex matches***
Input: /products/category/shoes
↪️ Output: 3

*** Single regex match***
Input: /home/
↪️ Output: 1
Regex PatternReturn Value
No Match Handler (optional)
💾 Value to return or function to call when no pattern matches.

Supported formats:
  ✓ Default value: [], ["unknown"]
  ✓ Callback function: str => ["No match for: " + str]
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the string before pattern matching (e.g., normalize case, clean whitespace).
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result array before returning it (e.g., arr => arr.join(',') to concatenate, arr => arr.length for count, arr => arr[0] for first match only). Useful for chaining transformations on the output.
Input String string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Regex Pattern Table table
Regex PatternReturn Value
No Match Handler (optional) string
regexTableLookupAll()


Under the Hood

📜 View Implementation Code
/**
 * Performs pattern matching against a string using a table of regex patterns and returns an array of all corresponding values for matching patterns.
 * 
 * @param {string} data.src - The input string to match against the regex patterns.
 * @param {Array} data.tbl - Array of objects with 'rex' and 'val' properties for regex matching.
 * @param {*|Function} [data.def] - Optional default value or callback function to handle cases when no pattern matches.
 * @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|any} An array of values associated with all matching patterns, or the result of the default handler if no matches are found.
 *
 * @framework ggLowCodeGTMKit
 */
const regexTableLookupAll = function(inputString, table, defaultOrCallback) {
    const test = function(stringToMatch, regexPattern) {
        return stringToMatch.search(regexPattern) !== -1;
    };
    
    const matches = [];
    
    for (var i = 0, len = table.length; i < len; i += 1) {
        if (test(inputString, table[i].rex)) {
            matches.push(table[i].val);
        }
    }
    
    // Handle no match case
    if (matches.length === 0) {
        if (typeof defaultOrCallback === 'function') {
            return defaultOrCallback(inputString);
        }
        if (defaultOrCallback !== undefined) {
            return defaultOrCallback;
        }
    }
    
    return matches;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// regexTableLookupAll - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(regexTableLookupAll(value, data.tbl, data.def));
// ===============================================================================
// regexTableLookupAll(...) – Apply Mode
// ===============================================================================
/*
return function(inputString) {
   return out(regexTableLookupAll(inputString, data.tbl, data.def));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Multiple regex matches'
✅ '[example] Single regex match'
✅ Test no matches with callback
✅ Test no matches without callback returns empty array
✅ Test all patterns match