I’m trying to clean up phone numbers coming from a webhook payload before they hit the routing logic. The input is consistently in E.164 format, like +13125551234. I need the output to be (312) 555-1234 for a legacy IVR integration that doesn’t handle the plus sign or country code.
I’ve been wrestling with the replace and substring functions in the Architect expression builder. My current attempt looks like this:
replace(
replace(
replace(
substring(${trigger.data.phone}, 2, 3),
"",
"("
),
"",
") "
),
"",
"-"
)
This is getting messy fast. The substring function seems to be 1-indexed in some contexts but 0-indexed in others, or maybe I’m just losing my mind. The issue is that replace doesn’t take a start index, so I can’t easily insert the parentheses and dash at specific positions. I tried using concat with multiple substrings:
concat(
"(",
substring(${trigger.data.phone}, 2, 3),
") ",
substring(${trigger.data.phone}, 5, 3),
"-",
substring(${trigger.data.phone}, 8, 4)
)
Wait, the indices are off. If the string is +13125551234, index 1 is 1, index 2 is 3. So substring(phone, 2, 3) grabs 312. That part works. But the expression builder throws a syntax error if I chain too many substring calls inside a concat. Is there a cleaner way to do this? Or am I stuck writing a Data Action just for regex replacement? The built-in string functions feel incredibly limited for simple formatting tasks.