Skip to content

Instantly share code, notes, and snippets.

@pwelter34
Created March 16, 2016 20:42
Show Gist options
  • Save pwelter34/d4d32189b870cf7601b4 to your computer and use it in GitHub Desktop.
Save pwelter34/d4d32189b870cf7601b4 to your computer and use it in GitHub Desktop.
format phone number
static formatPhone(phone: string): string {
var regexPhone = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
var regexExtension = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})[-. ]?[A-Za-z:. ]*([0-9]+)$/;
if (regexExtension.test(phone)) {
return phone.replace(regexExtension, "($1) $2-$3 x$4");
} else if (regexPhone.test(phone)) {
return phone.replace(regexPhone, "($1) $2-$3");
} else {
return phone;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment