Skip to content

test — GTM Variable Template for String

VARIABLES › STRING
test CORE String

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


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

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

GTM Configuration

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()


Under the Hood

📜 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