Skip to content

test β€” GTM Variable Template for String

VARIABLES β€Ί STRING
test CORE String
Direct (.tpl) Apply (.tpl)

Tests whether a regular expression pattern matches a string. Returns true if a match is found, false otherwise. Useful for validation and conditional logic.


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.


Pattern matches
INPUT
String to Search: Hello World
Regular Expression Pattern: World
OUTPUT
true
No match returns false
INPUT
String to Search: Hello World
Regular Expression Pattern: xyz
OUTPUT
false

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

test
String to Search
πŸ’Ύ The string to search within.

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., 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 with str => str.toLowerCase(), 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
test()


πŸ“œ View Implementation Code
/**
* Tests whether a given regular expression pattern matches the string.
*
* @param {string} data.str - The string to search within.
* @param {string} data.pat - The regular expression pattern to match in the 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 str before matching.
*
* @returns {boolean} Returns true if a match is found, false if no match is found.
*
* @framework ggLowCodeGTMKit
*/
const test = function(stringToMatch, regexPattern) {
if (typeof stringToMatch !== 'string' || typeof regexPattern !== 'string') {
return false;
}
return stringToMatch.search(regexPattern) !== -1;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// test - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedString = applyCast(data.pre, data.str);
return out(test(processedString, data.pat));
// ===============================================================================
// test(...) – Apply Mode
// ===============================================================================
/*
return function(stringToMatch, regexPattern) {
return out(test(stringToMatch, data.pat));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Pattern matches'
βœ… String with regex pattern match - returns true
βœ… '[example] No match returns false'
βœ… Case sensitive pattern no match - returns false
βœ… Empty pattern in string - returns true