Need Help with Reformatting a phone number

The form will provide me with the following format: +1 555 555 5555
I need it to be: +1 (555) 555 5555

I assume I have to use Text Parser (replace) and Regex.

Sure! Here’s a regular expression that should match a phone number in the format +1 555 555 5555 and capture the three groups of digits:

\+1 (\d{3}) \d{3} \d{4}

Here’s how it works:

  • \+1 matches the literal “+1”
  • ( and ) define a capture group, which captures the digits that follow
  • \d{3} matches three digits (0-9)
  • ``(a space) matches a literal space character
  • \d{3} and \d{4} match three and four digits, respectively

To reformat the phone number to the desired format, you can use a string replace function and reference the capture groups using the $1, $2, and $3 syntax. For example in Javascript:

const formattedPhoneNumber = originalPhoneNumber.replace(
/+1 (\d{3}) (\d{3}) (\d{4})/,
“+1 ($1) $2 $3”
);

=== ALL OF THE TEXT ABOVE THIS LINE WAS PROVIDED BY CHATGPT3, UNALTERED ===

But in Make

replace(<your string reference>; /\+1 (\d{3}) (\d{3}) (\d{4})/;+1 ($1) $2 $3)

Many standard JavaScript functions have a mirror in Make and work similarly if not exactly the same, and thankfully replace accepts regular expressions as the first argument as the search pattern.

3 Likes

You should also be able to use the built in Phone module in Make to parse the phone number and use many different output formats.

6 Likes