Architect expression to strip +1 and format phone number?

I’m trying to clean up some inbound phone numbers in an Architect flow. The data coming in from the IVR is just a raw string like +15551234567 and I need it formatted as (555) 123-4567 for the downstream CRM integration.

I’ve tried using the standard string functions in the expression editor, but it’s getting messy. I don’t want to write a whole sub-flow with multiple Set Variable blocks to parse each digit. I’m looking for a single expression that can handle the transformation.

Here is what I have so far in a Set Variable block:

phoneFormatted = SUBSTR(phoneRaw, 3, 10)

That gets me the digits without the country code, but formatting it with parentheses and dashes is where I’m stuck. The REPLACE function doesn’t seem to support regex or multiple replacement patterns in one go. I tried chaining them:

REPLACE(REPLACE(SUBSTR(phoneRaw, 3, 10), "(1)(3)(3)", "(\1) \2-\3"))

But that syntax isn’t valid in the Architect expression parser. It just throws a “Invalid expression” error when I try to validate the flow.

Is there a way to use a custom function or a more advanced string manipulation technique here? I know I could do this in a Data Action calling an external API, but that adds latency and complexity for something that should be simple string manipulation.

I’ve also looked into using the JavaScript SDK to handle this pre-IVR, but the flow is already built and I’d rather not refactor the whole thing if I can avoid it.

Any ideas on how to squeeze this formatting into a single Architect expression? I’m running out of hair on my head pulling this out.