Looking for advice on constructing a robust Architect expression to transform a phone number from E.164 format (+1XXXXXXXXXX) to standard US formatting ((XXX) XXX-XXXX).
Background
I am processing inbound webhook events in a Deno Deploy function. When specific criteria are met, I trigger an Architect flow to update a contact attribute. The raw phone number arrives as a string like +12125551234. I need to format this before passing it to a downstream API that strictly requires the (XXX) XXX-XXXX pattern. I am using the replace function with a regex capture group in an Architect Data Action.
Issue
The expression works for valid inputs but throws a null reference error or returns empty strings when the input field is null or contains non-numeric characters. My current expression is:
replace(replace(${input.phoneNumber}, "^\\+1", ""), "([0-9]{3})([0-9]{3})([0-9]{4})", "($1) $2-$3")
When input.phoneNumber is null, the flow fails validation. I tried wrapping it in an if statement:
if(isNull(${input.phoneNumber}), "", replace(replace(${input.phoneNumber}, "^\\+1", ""), "([0-9]{3})([0-9]{3})([0-9]{4})", "($1) $2-$3"))
However, the regex capture groups seem to behave inconsistently across different Architect versions, sometimes capturing the +1 if the initial replace doesn’t strip it cleanly due to whitespace.
Troubleshooting
- Verified the input is always a string type in the webhook payload.
- Tested the regex in an online tester, but Architect’s engine seems stricter with escape characters.
- Attempted to use
trim()before the replace, but it didn’t resolve the null handling issue.
Is there a more reliable way to handle nulls and ensure the regex matches exactly 10 digits after stripping the country code? Any working expression examples would be appreciated.