extract — GTM Variable Template for String
extract CORE String
Extracts from a string using regex. Returns first capture group if present, otherwise full match. Returns undefinedif no match 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
Extract numbers from string
INPUT
String To Extract From: Hello World 123
Regex Pattern: \\d+
Regex Pattern: \\d+
OUTPUT
123
Non-string input returns undefined
INPUT
String To Extract From: 12345
Regex Pattern: \\d+
Regex Pattern: \\d+
OUTPUT
undefined
GA cookie CID extraction
INPUT
String To Extract From: GA1.1.790863531.1759168565
Regex Pattern: \\d+\\.\\d+\\.(\\d+\\.\\d+)$
Regex Pattern: \\d+\\.\\d+\\.(\\d+\\.\\d+)$
OUTPUT
790863531.1759168565
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
extract
String To Extract From
Extracts from a string using regex. Returns first capture group if present, otherwise full match. Returns undefined if no match found.
*** Extract numbers from string***
Input: String To Extract From: Hello World 123
Regex Pattern: \d+
↪️ Output: 123
*** Non-string input returns undefined***
Input: String To Extract From: 12345
Regex Pattern: \d+
↪️ Output: undefined
*** GA cookie CID extraction***
Input: GA1.1.790863531.1759168565
↪️ Output: 790863531.1759168565
*** Extract numbers from string***
Input: String To Extract From: Hello World 123
Regex Pattern: \d+
↪️ Output: 123
*** Non-string input returns undefined***
Input: String To Extract From: 12345
Regex Pattern: \d+
↪️ Output: undefined
*** GA cookie CID extraction***
Input: GA1.1.790863531.1759168565
↪️ Output: 790863531.1759168565
Regex Pattern
💾 A regular expression pattern used for extraction.
Supported formats:
✓ String
The Regex Pattern input allows the definition of the regular expression to test for matches in the provided parameter. In this environment, a single backslash () is permitted within the regular expression.
Supports capturing groups, but does not support flag groups (e.g., (?i) for case-insensitivity).
The regular expression is case-sensitive by default.
✏️ Example Patterns:
d: Matches any digit (0-9).
w: Matches any word character (letters, digits, and underscores).
s: Matches any whitespace character (spaces, tabs, etc.).
Supported formats:
✓ String
The Regex Pattern input allows the definition of the regular expression to test for matches in the provided parameter. In this environment, a single backslash () is permitted within the regular expression.
Supports capturing groups, but does not support flag groups (e.g., (?i) for case-insensitivity).
The regular expression is case-sensitive by default.
✏️ Example Patterns:
d: Matches any digit (0-9).
w: Matches any word character (letters, digits, and underscores).
s: Matches any whitespace character (spaces, tabs, etc.).
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String To Extract From string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Regex Pattern string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
matchRegex()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Matches the provided string against the given regular expression pattern.
*
* @param {string} data.src - The string to search within.
* @param {string} data.rex - 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 src before matching.
*
* @returns {string|undefined} The first capture group if present, otherwise the full match. Returns undefined if no match is found or input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const matchRegex = function(stringToMatch, regexPattern) {
if (typeof stringToMatch !== 'string' || typeof regexPattern !== 'string') {
return undefined;
}
const match = stringToMatch.match(regexPattern);
if (match === null) return undefined;
// Return captured group if exists, otherwise full match
return match[1] !== undefined ? match[1] : match[0];
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// matchRegex - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(matchRegex(value, data.rex));
// ===============================================================================
// matchRegex(...) – Apply Mode
// ===============================================================================
/*
return function(value, regexPattern) {
regexPattern = data.rp1 ? data.rex : regexPattern;
return out(matchRegex(value, regexPattern));
};
*/🧪 View Test Scenarios (12 tests)
✅ '[example] Extract numbers from string'
✅ Email pattern match
✅ No match found
✅ Word pattern match
✅ '[example] Non-string input returns undefined'
✅ Invalid regex pattern (non-string)
✅ Empty string input
✅ UI-bound mode with static regex
✅ Multiple matches (returns first)
✅ Case-sensitive pattern
✅ '[example] GA cookie CID extraction'
✅ Capture group extraction - Email username only