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.
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
GTM Configuration
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
Same category: Array
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'