Skip to content

Instantly share code, notes, and snippets.

@xbrunosousa
Last active October 11, 2021 09:54
Show Gist options
  • Save xbrunosousa/a6179bccdfbf993b234e31e8afa80457 to your computer and use it in GitHub Desktop.
Save xbrunosousa/a6179bccdfbf993b234e31e8afa80457 to your computer and use it in GitHub Desktop.
Formata número de celular com DDI
const formatPhone = phone =>
phone
.replace(/\D/g, "") // Remove all NaN chars
.replace(/^(\d{2})(\d)/g, "+$1 $2") // Insert + in ddi and space on the end
.replace(/(\d{2})(\d)/, "($1) $2 ") // Insert () in ddd and space on the end
.replace(/(\d)(\d{4})$/, "$1-$2"); // Insert hyphen before 10º char
console.log("phone => ", formatPhone("5521999999999"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment