Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Created June 22, 2024 07:52
Show Gist options
  • Save wmakeev/d088a4bacd525b421559c68223c93491 to your computer and use it in GitHub Desktop.
Save wmakeev/d088a4bacd525b421559c68223c93491 to your computer and use it in GitHub Desktop.
[phone format] #phone #format
const formatPhone = (phone) => {
if (typeof phone !== "string") return "";
const phoneNums = phone.replaceAll(/\D/g, "");
if (phoneNums.length !== 11) return "+" + phoneNums;
const code = phoneNums.substring(0, 1);
const region = phoneNums.substring(1, 4);
const phone1 = phoneNums.substring(4, 7);
const phone2 = phoneNums.substring(7, 9);
const phone3 = phoneNums.substring(9, 11);
return `+${
code === "8" ? "7" : code
} (${region}) ${phone1}-${phone2}-${phone3}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment