1

I have tried Replace and ReplaceList functions, but instead of providing a specific list, is there a pattern that I can use with RegexMatch function and replace all the special characters like '\|!@#$%^&()_+={}":;?/>.,`~ /+-'?

I have tried the below but it is only replacing the first char,

 %%[

      set @str1 = 'ab ab'
      set @str1b = replace(@str1,regexmatch(@str1, '[a-zA-Z0-9]' , 0, "IgnoreCase", "Multiline"), "z")

 ]%%

output: zb zb

Or is there a way we can use RegexMatch in replaceList function? or an easy way to include all the special chars in the replaceList?

5

The below AMPscript will replace any character that is not a number or letter with a 'z'.

%%[
set @string = 'ab ab'
for @i = 1 to length(@string) do
  set @char = substring(@string,@i,1)
  set @result = iif(empty(regexmatch(@char,'[a-zA-Z0-9]',0)),concat(@result,'z'),concat(@result,@char)) 
next @i
output(concat("@result: ",@result))
]%%

Output: "@result: abzab"

This AMPscript will remove any characters that are not numbers or letters:

%%[
set @string = '@R[]{}e\/*!@#$%^&*()$" "g\|“”_+=e":…;‘x?I/s>.,`~F+u-?n123"'
for @i = 1 to length(@string) do
  set @char = substring(@string,@i,1)
  set @result = iif(empty(regexmatch(@char,'[a-zA-Z0-9]',0)),concat(@result,''),concat(@result,@char)) 
next @i
output(concat("@result: ",@result))
]%%

Output: "@result: RegexIsFun123"

  • I could never figure out how to do a for loop for all the chars inside AMPScript. Well done figuring out to utilize the string length. This can be cumbersome on some larger strings, but will be more effective then the SSJS method I showed on the majority – Gortonington Feb 12 '19 at 18:32
1

The following will do as you want, but it cannot be used in a sendable context - due to the SSJS do/while statement.

The issue is that the regexmatch will only return the first character it finds, it will not return a list of the chars e.g. there is no 'global' replace option like there is in SSJS.

This is the sample using do/while to complete it via AMPscript:

%%[
      set @str1 = 'ab^ ab%'
      set @regex = regexmatch(@str1, '[^a-zA-Z0-9\s]' , 0, "IgnoreCase", "Multiline")

]%%

<script runat=server>
var i = 1;

do{

</script>

%%[

      set @str1 = replace(@str1,@regex, "z")
      set @regex = regexmatch(@str1, '[^a-zA-Z0-9\s]' , 0, "IgnoreCase", "Multiline")
      set @index = Indexof(@str1,@regex)

]%%

regex: %%=v(@regex)=%%<br>
string: %%=v(@str1)=%%<br>
Index: %%=v(@index)=%%<br>

<script runat=server>
  Platform.Load("Core","1");
  var i = Variable.GetValue("@index");

  }while (i > 0)
</script>

OUTPUT:

regex: ^
string: abz ab%
Index: 7
regex: %
string: abz abz
Index: 0

If you are willing to do this via SSJS - which can be used in a sendable environment (just be aware it is much slower processing than AMPscript), you can use a global replace there:

<script runat=server>
var string = 'ab^ ab%'
string = string.replace(/[^a-zA-Z0-9\s]/g,'z')
Platform.Response.Write('jsString: ' + string);
</script>

OUTPUT: jsString: abz abz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.