Skip to content

findIndexByRegex β€” GTM Variable Template for Array

VARIABLES β€Ί ARRAY
findIndexByRegex EXTENDED Array
Direct (.tpl) Apply (.tpl)

Returns the index of the first element matching a regex pattern. Returns -1 if not found.



Find by regex pattern
INPUT
Input Array: ['apple', 'banana', 'apricot', 'cherry']
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
OUTPUT
-1

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"
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., 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
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
findIndexByRegex()


πŸ“œ 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'