replaceFirst β GTM Variable Template for String
replaceFirst CORE String
Replaces the first occurrence of a pattern in a string with a replacement value. Useful for simple text substitutions.
Examples
Section titled βExamplesβReplace first occurrence
INPUT
Input String: hello world hello
Pattern to Replace: hello
Replacement Value: hi
Pattern to Replace: hello
Replacement Value: hi
OUTPUT
hi world hello
Remove first occurrence
INPUT
Input String: test123test456
Pattern to Replace: test
Replacement Value:
Pattern to Replace: test
Replacement Value:
OUTPUT
123test456
Non-string input returns original
INPUT
Input String: 12345
Pattern to Replace: 2
Replacement Value: 9
Pattern to Replace: 2
Replacement Value: 9
OUTPUT
12345
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
replaceFirst
Input String
πΎ The input string where replacement will occur.
Supported formats:
β String
Supported formats:
β String
Pattern to Replace
πΎ The pattern to search for in the string. Supports both literal text and regex patterns.
Supported formats:
β String (literal): "Hello", "test"
β String (regex pattern): "d+", "^start", "end$"
β οΈ Escaping note: For regex patterns, use single backslash in this UI field (e.g.,
Supported formats:
β String (literal): "Hello", "test"
β String (regex pattern): "d+", "^start", "end$"
β οΈ Escaping note: For regex patterns, use single backslash in this UI field (e.g.,
d+ for digits). GTM automatically handles the escaping.Replacement Value
πΎ The replacement value to use. Use empty string to remove the first match.
Supported formats:
β String
Supported formats:
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before replacement (e.g., normalize case, trim whitespace).
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result string before returning it (e.g.,
str => str.toUpperCase(), str => str.trim()). Useful for chaining transformations on the output.Input String string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Pattern to Replace string
Replacement Value string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
replaceFirst()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Replaces the first occurrence of a pattern in a string with a replacement value.** @param {string} data.src - The input string where replacement will occur.* @param {string} data.pat - The pattern to search for in the string.* @param {string} data.rep - The replacement value to use.* @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 replacement.** @returns {string} The string with the first occurrence of the pattern replaced.** @framework ggLowCodeGTMKit*/const replaceFirst = function(input, pattern, replacement) { if (typeof input === 'string' && typeof pattern === 'string' && typeof replacement === 'string') { return input.replace(pattern, replacement); } return input;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// replaceFirst - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedInput = applyCast(data.pre, data.src);return out(replaceFirst(processedInput, data.pat, data.rep));// ===============================================================================// replaceFirst(...) β Apply Mode// ===============================================================================/*return function(value, pattern, replacement) { return out(replaceFirst(value, data.pat, data.rep));};*/π§ͺ View Test Scenarios (8 tests)
β
'[example] Replace first occurrence'β
Replace word in middle of string - should replace only first occurrenceβ
'[example] Remove first occurrence'β
Pattern not found - should return original stringβ
Empty string input - should return empty stringβ
Replace special characters - should replace first occurrenceβ
Replace with longer string - should work correctlyβ
'[example] Non-string input returns original'