Auto format phone number with dashes

PHOTO EMBED

Fri Dec 11 2020 14:26:09 GMT+0000 (Coordinated Universal Time)

Saved by @brannonglover #react.js,javascript

export function formatPhoneLink(
  phoneNumberString: string,
  useFormat: boolean = false
): string | null {
  const cleaned = `${phoneNumberString}`.replace(/\D/g, "");

  const match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/);
  if (useFormat && match) {
    const intlCode = match[1] ? "+1 " : "";
    return [intlCode, "(", match[2], ") ", match[3], "-", match[4]].join("");
  }
  return cleaned ?? null;

  // return null;
}
content_copyCOPY

Formatting a phone number