Last active
October 11, 2021 09:54
-
-
Save xbrunosousa/a6179bccdfbf993b234e31e8afa80457 to your computer and use it in GitHub Desktop.
Formata número de celular com DDI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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