replaceAll β GTM Variable Template for 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.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβReplace all occurrences
INPUT
Input String: hello world hello universe hello
Pattern To Replace: hello
Replacement Value: hi
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:
Pattern To Replace: -
Replacement Value:
OUTPUT
abcde
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.
replaceAll
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.
Supported formats:
β String
Supported formats:
β String
Replacement Value
πΎ The replacement value to use.
Supported formats:
β String
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
replaceAll()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled β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