# Regex\_replace to format phone numbers

**URL:** <https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717>\
**Category:** Data Hub\
**Tags:** format-data-actions\
**Created:** [November 9, 2023, 8:08pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717 "2023-11-09T20:08:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 9, 2023, 8:08pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/1 "2023-11-09T20:08:01Z")

</div>

Hi! Maybe you’ll know the answer to this Ops Hub question @karstenkoehler…

I want to format contact phone numbers from 8004567890 or 18004567890 to +1 (800) 456-7890.

I created a workflow and added the Format Data step. According to this [developer doc](https://developers.hubspot.com/docs/cms/hubl/filters#regex-replace), I should write the expression like this:

- {{ “[phone]”|regex\_replace(\d{3})(\d{3})(\d{4})', ‘$1-$2-$3’) }}

That, however, returns an error. So I rewrote the expression according to how the workflow wanted it, and it indicated no issues:

- regex\_replace([phone],‘^\D\*1?’, ‘+1’), ‘(\d{3})(\d{3})(\d{4})’, ‘$1-$2-$3’)

It didn’t work though 🙁 Running the workflow returns the error

 ![Screen Shot 2023-11-09 at 2.39.26 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/1X/510112b5c951dc6075022609399cad381ed86a3c.png)

So how are we to write this expression? Why is it required in a different format compared to HubDB?

---

<div class="post-metadata">

**Author:** ![Berangere](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/berangere/32/220784_2.png) [@Berangere](https://community.hubspot.com/u/Berangere)\
**Post date:** [November 10, 2023, 11:05am UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/2 "2023-11-10T11:05:18Z")

</div>

Hi @EmmaAtKiwi 👋, I hope that you are keeping well!  
Thank you for asking the Community, great question!  
I understand that you are using a custom coded workflow action to format phone numbers but you get an error. If that’s not correct, please let me know.  
**- When you click on “show details” regarding the error message, what does it say, please?**  
The more info, screenshots (without sensitive/confidential information), and details you can provide, the better the Community can assist.  
I’d like to invite a couple of subject matter experts to this conversation: Hi @louischausse, @ChrisoKlepke and @Teun do you have suggestions to help @EmmaAtKiwi, please?  
**Also, if anybody else has anything to add and/or share, please feel free to join in the conversation 🙂**  
Thank you very much for sharing your expertise ❤ and have a wonderful day!  
Best,  
Bérangère

---

<div class="post-metadata">

**Author:** ![Expertopinionsa](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@Expertopinionsa](https://community.hubspot.com/u/Expertopinionsa)\
**Post date:** [November 13, 2023, 4:44pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/3 "2023-11-13T16:44:10Z")

</div>

Hey there!

I get it, I think the expression you’re using is almost there but needs a little tweak. Try this instead

{{ “[phone]”|regex\_replace(‘^\D\*1?(\d{3})(\d{3})(\d{4})’, ‘+1 ($1) $2-$3’) }}

This will take care of formatting numbers like 8004567890 or 18004567890 into +1 (800) 456-7890.

The changes I made is adjusting the placement of the capturing groups and fixing the pattern for the optional ‘1’. Give it a shot, and hopefully, it solves the problem for you.

I would love know how it goes on triotech systems! If you still run into issues, we can dive deeper into troubleshooting. Good luck!

---

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 15, 2023, 4:56pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/4 "2023-11-15T16:56:58Z")

</div>

Thanks @Expertopinionsa! I’m still getting an error though, since HubSpot CRM using this function differently than HubSpot CMS, it seems.

 ![Screen Shot 2023-11-15 at 11.54.38 AM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/f/9f4099043bb10707f572035e6fc5596d0fa815f3.png)

It doesn’t like the {{… but when I remove the brackets, it doesn’t like |… even though that’s how they syntax is meant to be written!

 ![Screen Shot 2023-11-15 at 11.56.13 AM.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/f/f7b7f929fc4474992daed4e5dbda8a0072b25df1.png)

---

<div class="post-metadata">

**Author:** ![Expertopinionsa](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@Expertopinionsa](https://community.hubspot.com/u/Expertopinionsa)\
**Post date:** [November 23, 2023, 5:59pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/5 "2023-11-23T17:59:41Z")

</div>

Give it a shot and let me know if it works this time. Fingers crossed! 🤞 If not, we can keep tweaking until we nail it.

`"Phone number " regex_replace('^\D*1?(\d{3})(\d{3})(\d{4})', '+1 ($1) $2-$3')`

---

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 28, 2023, 10:42pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/6 "2023-11-28T22:42:31Z")

</div>

No luck yet 🙁

 ![886115_1_Screen Shot 2023-11-28 at 5.42.13 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/7/b/7b7798e3c11bfcf8517c2c92f7d36c3caa6b8e8d.png)

---

<div class="post-metadata">

**Author:** ![Berangere](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/berangere/32/220784_2.png) [@Berangere](https://community.hubspot.com/u/Berangere)\
**Post date:** [November 30, 2023, 11:42am UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/7 "2023-11-30T11:42:32Z")

</div>

Hi @EmmaAtKiwi, I hope that you are well!  
I have limited experience with coding so I’d like to ask some Top Experts to take a look at this: Hi @karstenkoehler, @KimRogers, @SR0, [@Ntbrown](https://community.hubspot.com/t5/user/viewprofilepage/user-id/118753), @Gonzalo and @jpsanchez do you have suggestions to help @EmmaAtKiwi on this, please?  
Thank you very much ❤ and have a nice day!  
Best,  
Bérangère

---

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 15, 2023, 4:51pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/8 "2023-11-15T16:51:30Z")

</div>

Thanks [@BérangèreL](https://community.hubspot.com/t5/user/viewprofilepage/user-id/230185)! Unfortunately, clicking on “show details” does not, in fact, give me any details… The error message only reiterates that I wrote the syntax wrong, but I don’t know where!

 ![Screen Shot 2023-11-15 at 11.49.25 AM.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/c/cd9b1082899e8661bc504d78ed9596fbc163b9e9.png)

---

<div class="post-metadata">

**Author:** ![SteveHTM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stevehtm/32/22074_2.png) [@SteveHTM](https://community.hubspot.com/u/SteveHTM)\
**Post date:** [November 18, 2023, 5:21pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/9 "2023-11-18T17:21:02Z")

</div>

Hu @EmmaAtKiwi - I may be confused, but it looks to me that you are trying to use the regex function in a workflow format step. But the reference you are making to the usage docs is for a HubL function.

I know very well that the differences/inconsistencies between HubSpot tools and different conexts can be maddening sometimes.

The (hard to search for) list of avalable formulas for the workflow context is at: [Use Custom Formula Functions when formatting data in workflows](https://knowledge.hubspot.com/workflows/custom-formula-functions) - I think you can do what you want with “replace” - but I haven’t tried specifically.

Steve

---

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 18, 2023, 7:12pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/10 "2023-11-18T19:12:05Z")

</div>

Hi @SteveHTM! You are correct, I AM trying to format phone numbers in a workflow, but can not figure out the correct syntax structure.

The developer docs I referenced indicated I could write the syntax like this --

{{ “[phone]”|regex\_replace(\d{3})(\d{3})(\d{4})', ‘$1-$2-$3’) }} -- but the workflow format step returned an error.

So how the heck am I supposed to write it, if the HubSpot workflow doesn’t seem to speak the same language as the HubSpot developer docs?!?!? 😩

---

<div class="post-metadata">

**Author:** ![Berangere](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/berangere/32/220784_2.png) [@Berangere](https://community.hubspot.com/u/Berangere)\
**Post date:** [November 20, 2023, 3:04pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/11 "2023-11-20T15:04:35Z")

</div>

Hi @EmmaAtKiwi,  
I understand how frustrating this can be.  
I’d like to ask our Top Experts: Hi @MBERARD, @HFisher7 and @louischausse do you have any tips to help @EmmaAtKiwi, please?  
@EmmaAtKiwi, if you don’t get an answer here, since this is quite technical, I recommend that you [post in the Developers Forum here](https://community.hubspot.com/t5/HubSpot-Developers/ct-p/developers).  
Thank you and have a great day!  
Best,  
Bérangère

---

<div class="post-metadata">

**Author:** ![SteveHTM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stevehtm/32/22074_2.png) [@SteveHTM](https://community.hubspot.com/u/SteveHTM)\
**Post date:** [November 20, 2023, 3:10pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/12 "2023-11-20T15:10:17Z")

</div>

@EmmaAtKiwi - I think my point is that your function reference is to a HubL document which really is a different language to the custom formula seup in workflow steps. So you can’t mix and match thos functions. HubL functions can only be used in page markup/HTML scripting etc.

Hope that this makes sense. Apologies if I am getting teh context incorrect.

Steve

---

<div class="post-metadata">

**Author:** ![EmmaAtKiwi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/emmaatkiwi/32/78422_2.png) [@EmmaAtKiwi](https://community.hubspot.com/u/EmmaAtKiwi)\
**Post date:** [November 28, 2023, 10:43pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/13 "2023-11-28T22:43:38Z")

</div>

Thank you @SteveHTM, you’re absolutely getting it correct, it’s ME that’s clearly struggling! So if my syntax deduced from the HubL doc won’t work, do you know what will?

---

<div class="post-metadata">

**Author:** ![Clare\_HS](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@Clare\_HS](https://community.hubspot.com/u/Clare_HS)\
**Post date:** [February 8, 2024, 12:52pm UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/14 "2024-02-08T12:52:22Z")

</div>

I know this thread is a little older, but I also know how frustrating it can be to not find a solution! [This is a bit more advanced](https://github.com/Antoinebr/HubSpot-Operations-Hub-Custom-Coded-Action/tree/master/format-us-phone-number), but I’ve found it works really well for what you’re trying to accomplish, @EmmaAtKiwi., and includes some instructions to help give context. You want the cca.js file to use as a custom code action instead of a formula…  
Gives two options for formatting US phone numbers. To get the +1, I just modifed line 74 slightly:

`return '+1 ' + '(' + match[1] + ') ' + match[2] + '-' + match[3];`

Here are two other screenshots that should help:

 ![Clare_HS_0-1707396222054.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/0/0e7fec0dafc74da8b6096b83f952ae745e058b75.png)

And…

 ![Clare_HS_1-1707396246665.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/d/4dbb1cb59621d096a9a0ec16c86e524865d44eca.png)

@AntoineBr, looks like this is yours, so I hope you don’t mind the respost. And THANK YOU for all the time you’re diligence will save me!!

---

<div class="post-metadata">

**Author:** ![DorianMcFarland](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dorianmcfarland/32/76702_2.png) [@DorianMcFarland](https://community.hubspot.com/u/DorianMcFarland)\
**Post date:** [February 13, 2024, 7:08am UTC](https://community.hubspot.com/t/regex-replace-to-format-phone-numbers/93717/15 "2024-02-13T07:08:45Z")

</div>

Hi @EmmaAtKiwi,

I was struggling with a similar issue. After many hours of frustration I found what appears to be a bug in the data formatting implementation of regex\_replace. I found that although the editor was accepting my input variable as a valid type, somewhere later down the line it was rejecting it. By forcing/coerding the input value into a string using another function I got it to work (I used “cut()” with no value). In my case I’m taking an 8 digit phone number and adding a country code:

`regex_replace(cut([phone], ""), "^(\d{8})$", "+47$1")`

In your case you’d want something like:

`regex_replace(cut([phone], ""), "(\d{3})(\d{3})(\d{4})", "+1 $1-$2-$3")`

Here’s the docs for the supported regex syntax: [Syntax · google/re2 Wiki · GitHub](https://github.com/google/re2/wiki/Syntax)

Although the workflow accepts **regex\_replace** as a valid function it’s not listed in the official documentation for custom formula functions:

> **[Use Custom Formula Functions when formatting data in workflows](https://knowledge.hubspot.com/workflows/custom-formula-functions)**
>
> Learn about the different functions you can use when formatting your data with a custom formula.

Hope that helps!
