Cleaning up a string using Smarts ASL

Someone recently asked how ASL can be used to remove unprintable characters from a string. This example shows one way of editing a string to remove any unwanted characters from it. The specific example shown below can be used in a DXA hook script to clean up the "EventText" of notifications.

// Standard hook script stuff - the notification name.
default NotificationName = "";
default MessageType = "";
START { .. eol } do {
        if (NotificationName == "") { return; }
        eventObj = self->object(NotificationName)

        // Clean up the event text.
        text = CLEAN( eventObj->EventText );
        eventObj->EventText = text;
}

// The "CLEAN" rule does the work..
CLEAN(str) {
        local output; local ch;
        input = str;
        delim = "";
        do { output = ""; }
        rep(
                // For each character in the input -- check: is it
                // a "good char"? If so -- append it to the output,
                // but if not - just discard it.
                {ch: GOOD_CHAR do { output = output.ch; }}
                | char
        )?
        // Return the output thus built up.
        do { return output; }
}

// The "GOOD_CHAR" rule matches characters that we have defined as
// "good". Adjust this to allow or dis-allow characters to taste.
GOOD_CHAR {
         any("0123456789")
        | any("abcdefghijklmnopqrstuvwxyz")
        | any("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        | any("\"!$%^&*()_-+={[}]:;@'~#?/>.<,| ")
}
Scroll to Top