Can AMPscript be used to find HTML within the body and delete/replace it? Trying to do this in Salesforce Content Builder.

For instance, find <span style="color:red;"></span> and delete/replace it. Similar to removeAttribute() in JavaScript.

3 Answers 3


No, unless the HTML is contained in a content block or a data extension column value. If it were, then you could utilize the AMPscript string functions on it -- functions like Replace, IndexOf, BuildRowSetFromString, etc.

AMPscript is a server-side language. Its functions are compiled/evaluated before the email is sent. It's similar to PHP and not client-side JavaScript in your browser.


Since AMPscript is a server-side language, you can only do the following:

%%[ IF @MY_VAR == "True" THEN ]%%

<span style="color:red;"></span>

%%[ ENDIF ]%%

Otherwise, here is another technique to think about:


SET @MY_VAR = "hidden"


var spans = document.querySelectorAll(".red");

for(var i = 0; i < spans.length; i++) {




As other person mentioned, no, it can't be done that way. Better way to think about it is to use AMPScript to include the HTML when the opposite of the deletion/replacment criteria is true.

So, if you want to delete HTML when LastName='Jones', use AMPScript to show the HTML when LastName!='Jones'. Any logic you want to use to delete can be NOTted to show instead of delete. For replacement just figure out the logic to determine what to show when the criteria you would have used to replace is True.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .