replaceAllWithRegex β GTM Variable Template for String
replaceAllWithRegex CORE String
Replaces all occurrences of a regular expression pattern in a string with a replacement value. Useful for pattern-based text transformations and data cleaning.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Filtering
Select or exclude items from collections based on criteria or predicates.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
Examples
Section titled βExamplesβMask all numbers
INPUT
Input String: Order 123 and Order 456
Regular Expression Pattern: \\d+
Replacement Value: XXX
Regular Expression Pattern: \\d+
Replacement Value: XXX
OUTPUT
Order XXX and Order XXX
Normalize whitespace
INPUT
Input String: hello world test
Regular Expression Pattern: \\s+
Replacement Value:
Regular Expression Pattern: \\s+
Replacement Value:
OUTPUT
hello world test
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.
replaceAllWithRegex
Input String
πΎ The input string where replacement will occur.
Supported formats:
β String
Supported formats:
β String
Regular Expression Pattern
π The regular expression pattern to search for in the string. All occurrences will be replaced.
Supported formats:
β String (regex pattern)
β οΈ Escaping note: In this UI field, use single backslash (e.g.,
Supported formats:
β String (regex pattern)
β οΈ Escaping note: In this UI field, use single backslash (e.g.,
d+ for digits). GTM automatically handles the escaping.Replacement Value
πΎ The replacement value to use. Use empty string to remove matches.
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.trim(), str => str.toUpperCase()). 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
Regular Expression Pattern string
Replacement Value string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
replaceAllWithRegex()
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 regular expression pattern in a string with a replacement value. * * @param {string} data.src - The input string where replacement will occur. * @param {string} data.ptn - The regular expression 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 regex pattern replaced. * * @framework ggLowCodeGTMKit */const matchAll = function(stringToMatch, regexPattern) { const matches = []; let index = 0; while (index < stringToMatch.length) { const match = stringToMatch.substring(index).match(regexPattern); if (match) { matches.push(match[0]); index += match.index + match[0].length; } else { break; } } return matches.length > 0 ? matches : null;};
const replaceAllWithRegex = function(input, pattern, replacement) { if (typeof input !== 'string' || typeof pattern !== 'string' || typeof replacement !== 'string') { return input; }
const matches = matchAll(input, pattern);
if (matches === null) { return input; // No matches found }
let result = input;
for (let i = 0; i < matches.length; i++) { result = result.replace(matches[i], replacement); }
return result;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// replaceAllWithRegex - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedInput = applyCast(data.pre, data.src);return out(replaceAllWithRegex(processedInput, data.ptn, data.rep));// ===============================================================================// replaceAllWithRegex(...) β Apply Mode// ===============================================================================/*return function(input, pattern, replacement) { return out(replaceAllWithRegex(input, data.ptn, data.rep));};*/π§ͺ View Test Scenarios (14 tests)
β
Test with non-string input returns original valueβ
'[example] Mask all numbers'β
Test removing dollar signsβ
'[example] Normalize whitespace'β
Test replacing word charactersβ
Test with no matches returns original stringβ
Test replacing special characters with escape sequencesβ
Test replacing emails with placeholderβ
Test with non-string pattern returns original stringβ
Test with non-string replacement returns original stringβ
Test replacing line breaksβ
Test case-insensitive patternβ
Test with null string returns original valueβ
Test with undefined string returns original value