regexTableLookupAll β GTM Variable Template for Value
regexTableLookupAll CORE Value
Matches a value against a table of regex patterns and returns all matching results.
When to Use This
Section titled β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
Section titled β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'}
]
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'}
]
Regex Pattern Table: [
{rex: '^/home/?$', val: 'Home Page'},
{rex: '/products/', val: 'Products'},
{rex: '/about/', val: 'About'}
]
OUTPUT
1
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.
regexTableLookupAll
Input String
πΎ The input string to match against regex patterns.
Supported formats:
β String
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.,
Example:
β’ Pattern:
β’ Pattern:
*** Multiple regex matches***
Input: /products/category/shoes
βͺοΈ Output: 3
*** Single regex match***
Input: /home/
βͺοΈ Output: 1
β οΈ 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]
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
regexTableLookupAll()
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 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