Skip to content

replaceAll — GTM Variable Template for String

VARIABLES › STRING
replaceAll EXTENDED String

Replaces all occurrences of a pattern in a string with a replacement value. Useful for comprehensive text substitutions and data cleaning operations.



Examples

Replace all occurrences
INPUT
Input String: hello world hello universe hello
Pattern To Replace: hello
Replacement Value: hi
OUTPUT
hi world hi universe hi
Remove all dashes
INPUT
Input String: a-b-c-d-e
Pattern To Replace: -
Replacement Value:
OUTPUT
abcde
Non-string input returns original
INPUT
Input String: 12345
Pattern To Replace: 2
Replacement Value: 9
OUTPUT
12345

GTM Configuration

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

replaceAll
Input String
💾 The input string where replacement will occur.

Supported formats:
  ✓ String
Pattern To Replace
💾 The pattern to search for in the string.

Supported formats:
  ✓ String
Replacement Value
💾 The replacement value to use.

Supported formats:
  ✓ String
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.
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
replaceAll()


Under the Hood

📜 View Implementation Code
/**
 * Replaces all occurrences 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 all occurrences of the pattern replaced.
 *
 * @framework ggLowCodeGTMKit
 */
const replaceAll = function(input, pattern, replacement) {
    if (typeof input === 'string' && typeof pattern === 'string' && typeof replacement === 'string') {
        let result = input;
        let index = result.indexOf(pattern);
        while (index !== -1) {
            result = result.substring(0, index) + replacement + result.substring(index + pattern.length);
            index = result.indexOf(pattern, index + replacement.length);
        }
        return result;
    }
    return input;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// replaceAll - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedInput = applyCast(data.pre, data.src);
return out(replaceAll(processedInput, data.pat, data.rep));
// ===============================================================================
// replaceAll(...) – Apply Mode
// ===============================================================================
/*
return function(input, pattern, replacement) {
   return out(replaceAll(input, data.pat, data.rep ));
};
*/
🧪 View Test Scenarios (6 tests)
✅ '[example] Replace all occurrences'
✅ '[example] Remove all dashes'
✅ Test pattern not found returns original string
✅ Test replacing with longer string
✅ '[example] Non-string input returns original'
✅ Test with non-string pattern returns original string