2

I'm currently learning Ampscript and SSJS for an email project. I'm trying to remove special characters to a specific object that is being called. I've came across some things that I think that could help but isnt sure how to use them.

Creating a template for an exact target email. Thought I could use the string.replace as show below. What would be some other options if any?

string = string.replace(/[^a-zA-Z0-9]/g, '');

Current code section below : the @img automatically pull content with special characters that needs to be removed before being used in the email

If RowCount(@customTemplate) != 0 Then
    Set @costumTemplate = True
    Set @img = Lowercase(Replace(@first_artist, ' ','_'))
  ElseIf RowCount(@locationTemplate) != 0 Then
    Set @locationTemplate = True
    Set @img = Lowercase(Replace(@venue_name,' ','_'))
  EndIf
4
  • Just to clarify, is that expression working for you already? You're just asking if there's another way to implement the same thing?
    – Adrian Larson
    Aug 29, 2016 at 18:07
  • It works however its not pulling the objects with non special character names.
    – FreeDaGeek
    Aug 29, 2016 at 18:24
  • Not pulling them? Maybe it's more clear to our marketing-cloud experts, but I'm not sure what you mean.
    – Adrian Larson
    Aug 29, 2016 at 18:26
  • 1
    Can you update your question with an example of a string that's not getting handled properly? Aug 29, 2016 at 19:29

1 Answer 1

2

I'd advise against using SSJS in your emails, especially since there is a native AMPScript function for doing the same thing.

Here are a few regex examples from my blog:

%%[
var @s, @o, @p, @m

outputLine(concat("<h4>Strip leading zeroes from a string</h4>"))
set @s = "0000012345"
set @p = "^0*(\d+)$"
set @o = RegExMatch(@s, @p, 1)
outputLine(concat("input:  ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

outputLine(concat("<h4>Check for all digits, no match</h4>"))
set @s = "12345x"
set @p = "^\d*$"
set @o = RegExMatch(@s, @p , 0)
outputLine(concat("input: ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

outputLine(concat("<h4>Check for all digits, match</h4>"))
set @s = "12345"
set @p = "^\d*$"
set @o = RegExMatch(@s, @p , 0)
outputLine(concat("input: ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

outputLine(concat("<h4>Replace parenthetical text with space</h4>"))
set @s = "whee (whatever it is) whoop"
set @p = "\s\(.+\)\s"
set @m = RegExMatch(@s, @p, 0 , "IgnoreCase")
set @o = replace(@s,@m, " ")
outputLine(concat("input:  ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("match: ",@m,"<br>"))
outputLine(concat("replaced: ",@o,"<br>"))

]%%

Reference:

RegExMatch() Function

4
  • Thanks! The original create used both ampscript * SSJS. Since I'm new with ampscript I wasn't aware there was a way to remove special characters in ampscript.
    – FreeDaGeek
    Aug 31, 2016 at 19:03
  • after the its matched how could I remove it from the string before I use it
    – FreeDaGeek
    Sep 1, 2016 at 17:47
  • you just use the replace() function -- like in the last example Sep 1, 2016 at 19:18
  • It doesn't seem to be working. I'm either doing it wrong or maybe its the fact I cant find a pattern to match Accent letters like a few of these: [link](bit.ly/1woX9jY ) Or maybe I'm explaining myself correctly. I'm trying to make sense of it at this point
    – FreeDaGeek
    Sep 8, 2016 at 22:01

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.