Comparison
regexTableLookupCallback CORE Value
Matches a value against regex patterns and returns the callback result of the first match.
When to Use This
Extraction
Pull specific values, segments, or patterns from complex data structures.
Examples
First matching pattern
INPUT
String to Match: /home/
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'}
]
func: func
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'}
]
func: func
OUTPUT
Home Page
No match returns undefined
INPUT
String to Match: /contact/
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'}
]
func: func
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'}
]
func: func
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
regexTableLookupCallback
String to Match
💾 The string to match against regex patterns.
Supported formats:
✓ String
Supported formats:
✓ String
Regex Pattern Table
📋 Table of regex patterns and their corresponding return values. The first matching pattern wins.
⚠️ Escaping note: In the regex pattern column, use single backslash (e.g.,
Example:
• Pattern:
• Pattern:
*** First matching pattern***
Input: /home/
↪️ Output: Home Page
*** No match returns undefined***
Input: /contact/
↪️ Output: undefined
⚠️ Escaping note: In the regex pattern column, use single backslash (e.g.,
d+ for digits). GTM automatically handles the escaping.Example:
• Pattern:
^/home/?$ → Value: Home Page• Pattern:
/products/ → Value: Products*** First matching pattern***
Input: /home/
↪️ Output: Home Page
*** No match returns undefined***
Input: /contact/
↪️ Output: undefined
Regex PatternReturn Value
⊖
⊖
No Match Handler (optional)
💾 Value to return or function to call when no pattern matches.
Supported formats:
✓ Default value: "unknown", 0
✓ Callback function: str => "No match for: " + str
Supported formats:
✓ Default value: "unknown", 0
✓ 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 URL path).
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the matched value before returning it (e.g.,
val => val.toUpperCase(), val => 'Matched: ' + val). Useful for chaining transformations on the output.String to Match 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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
regexTableLookup()
Related Variables
Same category: Value
Under the Hood
📜 View Implementation Code
/**
* Performs pattern matching against a string using a table of regex patterns and returns the corresponding value for the first match.
*
* @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 {string|any} The value associated with the first matching pattern, or the result of the default handler if no match is found.
*
* @framework ggLowCodeGTMKit
*/
const regexTableLookup = function(inputString, table, defaultOrCallback) {
const test = function(stringToMatch, regexPattern) {
return stringToMatch.search(regexPattern) !== -1;
};
for (var i = 0, len = table.length; i < len; i += 1) {
if (test(inputString, table[i].rex)) {
return table[i].val;
}
}
// Handle no match case
if (typeof defaultOrCallback === 'function') {
return defaultOrCallback(inputString);
}
return defaultOrCallback;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// regexTableLookup - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(regexTableLookup(value, data.tbl, data.def));
// ===============================================================================
// regexTableLookup(...) – Apply Mode
// ===============================================================================
/*
return function(inputString) {
return out(regexTableLookup(inputString, data.tbl, data.def));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] First matching pattern'
✅ Test second pattern matches when first doesn't
✅ No match returns result from no match handler function
✅ '[example] No match returns undefined'
✅ Test with numeric pattern matching