findIndexByRegex β GTM Variable Template for Array
findIndexByRegex EXTENDED Array
Returns the index of the first element matching a regex pattern. Returns -1 if not found.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβFind by regex pattern
INPUT
Input Array: ['apple', 'banana', 'apricot', 'cherry']
Regex Pattern: ^ap
Start Index (optional): 0
Regex Pattern: ^ap
Start Index (optional): 0
OUTPUT
0
No match returns -1
INPUT
Input Array: ['apple', 'banana', 'cherry']
Regex Pattern: ^orange
Start Index (optional): 0
Regex Pattern: ^orange
Start Index (optional): 0
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.
findIndexByRegex
Input Array
πΎ An array of strings or a comma-separated string.
Supported formats:
β Array: ["item1", "item2"]
β String: "item1,item2,item3"
Supported formats:
β Array: ["item1", "item2"]
β String: "item1,item2,item3"
Regex Pattern
π A regex pattern string to match against array items.
Supported formats:
β String (regex pattern): "^abc", "value$", "d+"
β οΈ Escaping note: In this UI field, use single backslash (e.g.,
Supported formats:
β String (regex pattern): "^abc", "value$", "d+"
β οΈ Escaping note: In this UI field, use single backslash (e.g.,
d+ for digits). GTM automatically handles the escaping.Start Index (optional)
πΎ Optional index to start searching from. Default is 0 (search from beginning).
Supported formats:
β Number: 0, 1, 2
Supported formats:
β Number: 0, 1, 2
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input array before searching (e.g., filter items, normalize values).
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result index before returning it (e.g.,
idx => idx !== -1 for boolean, idx => idx + 1 for 1-based indexing). Useful for chaining transformations on the output.Input Array array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Regex Pattern string
Start Index (optional) number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
findIndexByRegex()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Finds the first index in an array where an item matches a regex pattern. * * @param {Array|string} data.src - An array of strings or a comma-separated string. * @param {string} data.ptn - A regex pattern string (e.g., "^abc", "value$"). * @param {number} [data.idx] - Optional index to start searching from (default: 0). * @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 searching. * * @returns {number} The index of the first matching item, or -1 if no match is found. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const findIndexByRegex = function(array, pattern, fromIndex) { const arr = getType(array) === 'string' ? array.split(',') : array;
if (getType(arr) !== 'array') { return -1; }
const startIndex = typeof fromIndex === 'number' ? fromIndex : 0;
for (let i = startIndex; i < arr.length; i++) { const item = arr[i]; if (typeof item === 'string' && item.match(pattern)) { return i; } }
return -1;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// findIndexByRegex - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedArray = applyCast(data.pre, data.src);return out(findIndexByRegex(processedArray, data.ptn, data.idx));// ===============================================================================// findIndexByRegex(...) β Apply Mode// ===============================================================================/*return function(array, pattern, fromIndex) { return out(findIndexByRegex(array, data.ptn , data.idx ));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Find by regex pattern'β
Test finding item that ends with patternβ
Test with fromIndex to skip first matchesβ
Test with comma-separated string inputβ
'[example] No match returns -1'