Comparison
regexTableLookupCallback CORE Value
Matches a value against regex patterns and returns the callback result of the first match.
When to Use This
Section titled βWhen to Use ThisβExtraction
Pull specific values, segments, or patterns from complex data structures.
Examples
Section titled β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
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled β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