Runtime verified Differs from official docs Test scripts included

Syntax

Replace(sourceString, searchSubstring, replacementSubstring)  →  string
3 arguments — exactly

Parameters

Name Type Required Description
sourceString string Yes String to search in
searchSubstring string Yes Text to look for
replacementSubstring string Yes Text to put in its place; pass "" to delete the match

Exactly three arguments. Pass them as strings: a number or boolean literal in any position breaks the render.

Example

%%=Replace("Hello World", "World", "There")=%%

Renders Hello There.

Every occurrence is rewritten, which makes it handy for turning a delimited value into readable text:

%%=Replace("one,two,three", ",", " / ")=%%

Renders one / two / three.

Return value

string — the source with every match rewritten.

A source without a match comes back unchanged, and an empty source gives an empty string. Otherwise the result is free text, so there is no fixed set of special values to test for.

Behaviour

Three arguments, always. An email containing Replace("abc", "b") cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”, pointing at the call. To delete a fragment, pass an empty string as the third argument: Replace("a-b-c", "-", "") gives abc. Calls with no argument, one argument or four arguments are rejected at save the same way.

Every occurrence is replaced. Replace("one,two,three", ",", " / ") rewrites both commas. A search value equal to the whole source works too: Replace("abc", "abc", "xyz") gives xyz.

No match leaves the source as it was. Replace("Hello World", "zzz", "!") gives Hello World, and a search value longer than the source, Replace("ab", "abcdef", "z"), gives ab.

Empty values do nothing harmful. An empty search value is a no-op: Replace("abc", "", "X") and Replace("abc", "", "") both give abc. An empty source gives an empty string.

Numbers and booleans break the render. The email saves, but the preview fails (HTTP 500) when any argument is a bare number or boolean: Replace(101101, "0", "9"), Replace("a1b1c", 1, "-"), Replace("a-b", "-", 7), Replace(true, "ru", "X"), Replace("is true here", true, "X") and Replace("a-b", "-", true) all failed. Quote numeric values.

Non-ASCII text is replaced and inserted as written. In a source built as Concat("caf", Char(233), " time"), replacing Char(233) with e gives cafe time, and Replace("cafe time", "e t", Concat(Char(233), " T")) gives café Time.

Casing is ignored, and the source is scanned once

Call Renders
Replace("Hello World", "WORLD", "There") Hello There
Replace("Hello World", "world", "There") Hello There
Replace("Hello World", "hELLO", "Howdy") Howdy World
Replace("Cat cat CAT", "cat", "dog") dog dog dog
Replace("aaa", "aa", "a") aa
Replace("cat", "cat", "cat dog") cat dog

The search value matches regardless of case. The scan runs once from left to right and never looks again at text it has just inserted, so aaa becomes aa rather than a, and a replacement that contains the search value does not loop.

Show Next test script
<!-- Marketing Cloud Next test script for Replace. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[Hello There] got=[%%=Replace("Hello World","World","There")=%%]</p>
<p>c2 expect=[one / two / three] got=[%%=Replace("one,two,three", ",", " / ")=%%]</p>
<p>c3 expect=[aa] got=[%%=Replace("aaa","aa","a")=%%]</p>
<p>c4 expect=[xyz] got=[%%=Replace("abc", "abc", "xyz")=%%]</p>
<p>c5 expect=[Hello World] got=[%%=Replace("Hello World", "zzz", "!")=%%]</p>
<p>c6 expect=[ab] got=[%%=Replace("ab", "abcdef", "z")=%%]</p>
<p>c7 expect=[Hello There] got=[%%=Replace("Hello World", "WORLD", "There")=%%]</p>
<p>c8 expect=[Hello There] got=[%%=Replace("Hello World", "world", "There")=%%]</p>
<p>c9 expect=[Howdy World] got=[%%=Replace("Hello World", "hELLO", "Howdy")=%%]</p>
<p>c10 expect=[dog dog dog] got=[%%=Replace("Cat cat CAT", "cat", "dog")=%%]</p>
<p>c11 expect=[abc] got=[%%=Replace("a-b-c", "-", "")=%%]</p>
<p>c12 expect=[cat dog] got=[%%=Replace("cat", "cat", "cat dog")=%%]</p>
<p>c13 expect=[abc] got=[%%=Replace("abc", "", "X")=%%]</p>
<p>c14 expect=[abc] got=[%%=Replace("abc", "", "")=%%]</p>
<p>c15 expect=[] got=[%%=Replace("", "x", "y")=%%]</p>
<p>c16 expect=[cafe time] got=[%%=Replace(Concat("caf",Char(233)," time"), Char(233), "e")=%%]</p>
<p>c17 expect=[café Time] got=[%%=Replace("cafe time", "e t", Concat(Char(233), " T"))=%%]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the save is rejected (HTTP 400) -->
%%=Replace("abc","b")=%%

<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace(101101, "0", "9")=%%

<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace("a1b1c", 1, "-")=%%

<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace("a-b", "-", 7)=%%

<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace(true, "ru", "X")=%%

<!-- email 6: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace("is true here", true, "X")=%%

<!-- email 7: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Replace("a-b", "-", true)=%%

<!-- email 8: expect that the save is rejected (HTTP 400) -->
%%=Replace()=%%

<!-- email 9: expect that the save is rejected (HTTP 400) -->
%%=Replace("abc")=%%

<!-- email 10: expect that the save is rejected (HTTP 400) -->
%%=Replace("abc", "b", "z", "extra")=%%

Differs from official docs

  • Matching ignores case. The reference does not say so; in Next Replace("Cat cat CAT", "cat", "dog") gives dog dog dog. Card
  • Numbers and booleans break the render. The reference does not rule them out, but Replace("a-b", "-", 7) and Replace(101101, "0", "9") made the Next preview fail with HTTP 500. Card

Availability & differences

PlatformAvailable
Marketing Cloud EngagementYes
Marketing Cloud NextYes, from API 67.0

See also