sanitizeTextAndRemoveHtml — GTM Variable Template for String
sanitizeTextAndRemoveHtml CORE String
Sanitizes a string by removing HTML tags, replacing line breaks and tabs with spaces, collapsing multiple whitespace characters, and trimming leading/trailing whitespace.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Examples
Remove HTML tags
INPUT
Text To Sanitize: Hello World
OUTPUT
Hello World
Collapse multiple spaces
INPUT
Text To Sanitize: Hello World Test
OUTPUT
Hello World Test
Non-string input returns undefined
INPUT
Text To Sanitize: 12345
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
sanitizeTextAndRemoveHtml
Text To Sanitize
💾 The string to sanitize and clean.
Supported formats:
✓ String
Supported formats:
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., normalize encoding, convert to string). Internal transformations such as HTML removal will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' cleaned' for string modifications). Useful for chaining transformations on the output.
Text To Sanitize string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
sanitizeTextAndRemoveHtml()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Sanitizes a string by:
* - Removing HTML tags
* - Replacing line breaks and tabs with spaces
* - Collapsing multiple whitespace characters into a single space
* - Trimming leading/trailing whitespace
*
* @param {string} data.src - The string to sanitize.
* @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 sanitizing.
*
* @returns {string|undefined} The cleaned string, or undefined if input is not a string.
*
* @framework ggLowCodeGTMKit
*/
const sanitizeTextAndRemoveHtml = function(input) {
if (typeof input !== 'string') {
return undefined;
}
const blockTags = ['div', 'p', 'br', 'li', 'td', 'th', 'section', 'article'];
let noTags = '';
let insideTag = false;
let tagBuffer = '';
for (let i = 0; i < input.length; i++) {
const char = input[i];
if (char === '<') {
insideTag = true;
tagBuffer = '';
continue;
}
if (char === '>') {
insideTag = false;
const tagName = tagBuffer.toLowerCase().split(' ')[0].replace('/', '');
if (blockTags.indexOf(tagName) !== -1) {
noTags += ' ';
}
continue;
}
if (insideTag) {
tagBuffer += char;
} else {
noTags += char;
}
}
let cleaned = noTags.split('\n').join(' ')
.split('\r').join(' ')
.split('\t').join(' ');
while (cleaned.indexOf(' ') !== -1) {
cleaned = cleaned.split(' ').join(' ');
}
return cleaned.trim();
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// sanitizeTextAndRemoveHtml - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(sanitizeTextAndRemoveHtml(value));
// ===============================================================================
// sanitizeTextAndRemoveHtml() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(sanitizeTextAndRemoveHtml(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Remove HTML tags'
✅ String with line breaks and tabs - should replace with spaces
✅ '[example] Collapse multiple spaces'
✅ Complex HTML with block tags - should add spaces for block elements
✅ '[example] Non-string input returns undefined'