Skip to content

replaceFirst β€” GTM Variable Template for String

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

Replaces the first occurrence of a pattern in a string with a replacement value. Useful for simple text substitutions.


Replace first occurrence
INPUT
Input String: hello world hello
Pattern to Replace: hello
Replacement Value: hi
OUTPUT
hi world hello
Remove first occurrence
INPUT
Input String: test123test456
Pattern to Replace: test
Replacement Value:
OUTPUT
123test456
Non-string input returns original
INPUT
Input String: 12345
Pattern to Replace: 2
Replacement Value: 9
OUTPUT
12345

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
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., 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
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
replaceFirst()


πŸ“œ 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'