search — GTM Variable Template for String
search CORE String
Searches for a regular expression pattern in a string and returns the index of the first match. Returns -1 if no match is found.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
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
Find digit position
INPUT
String to Search: Hello World
Regular Expression Pattern: World
Regular Expression Pattern: World
OUTPUT
6
No match returns -1
INPUT
String to Search: Hello World
Regular Expression Pattern: xyz
Regular Expression Pattern: xyz
OUTPUT
-1
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
search
String to Search
💾 The string to search within.
Supported formats:
✓ String
Supported formats:
✓ String
Regular Expression Pattern
🔍 The regular expression pattern to search for. Returns the index of the first match.
Supported formats:
✓ String (regex pattern): "d+", "[a-z]+", "test"
⚠️ Escaping note: In this UI field, use single backslash (e.g.,
Supported formats:
✓ String (regex pattern): "d+", "[a-z]+", "test"
⚠️ Escaping note: In this UI field, use single backslash (e.g.,
d+ for digits). GTM automatically handles the escaping.Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the string before searching (e.g., normalize case, trim whitespace).
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result index before returning it (e.g.,
idx => idx !== -1 for boolean check, idx => idx + 1 for 1-based indexing). Useful for chaining transformations on the output.String to Search string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Regular Expression Pattern string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
search()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Simulates the behavior of String.prototype.search() using a regular expression literal.
*
* @param {string} data.src - The string to search within.
* @param {string} data.rgx - The regular expression pattern as a string.
* @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} Returns the index of the first match, or -1 if no match is found.
*
* @framework ggLowCodeGTMKit
*/
const search = function(searchData, regexPattern) {
if (typeof searchData !== 'string' || typeof regexPattern !== 'string') {
return -1;
}
return searchData.search(regexPattern);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// search - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(search(value, data.rgx));
// ===============================================================================
// search(...) – Apply Mode
// ===============================================================================
/*
return function(searchData, regexPattern) {
return out(search(searchData, data.rgx));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Find digit position'
✅ String with pattern at beginning - returns 0
✅ '[example] No match returns -1'
✅ String with regex pattern - finds match using regex
✅ Non-string input - returns -1