Phone Number Formatting in Set Node

I’m currently formatting a phone number to basically remove anything but numbers via a Set. This works fine, but I’m wanting to expand upon this and I’m wondering if it’s something I can do via Set or will I need to do this via a Function. I’m doing this:


But I want to be able to also add +1 to the beginning of the number and remove any leading 1s in the string (to then replace with +1). I’ve seen several ways of doing this via Javascript, but most of them involve a function of some sort and I’m hoping to keep this within Set. Thank you.

How about something like:

{{'+1' + $json["phonenumber"].replace(/\D/g, '').replace(/^1/, '')}}

Yup I think that works great. Any thoughts on dealing with an empty input? Right now this would add +1 even if the $json[“phonenumber”] is empty.

You can make an “if” in expression

You can use the ternary operator to perform a check on the phone number before you manipulate it:

{{ $json["phonenumber"] ? '+1' + $json["phonenumber"].replace(/\D/g, '').replace(/^1/, '') : undefined }}

I’m passing undefined in the false condition which actually removes the property, but you could pass an empty string like ‘’ instead to have the property come through empty.

Perfect, thank you very much!

1 Like