Skip to content

Instantly share code, notes, and snippets.

@wizard04wsu
Created May 1, 2014 18:37
Show Gist options
  • Save wizard04wsu/11458504 to your computer and use it in GitHub Desktop.
Save wizard04wsu/11458504 to your computer and use it in GitHub Desktop.
Regular Expressions for U.S. Phone Numbers
//Regular Expressions for U.S. Phone Numbers
//
//See http://en.wikipedia.org/wiki/North_American_Numbering_Plan
//
//Spaces, dashes, or periods are allowed as separators.
//Extensions can be recognized by several strings: #, x, x., ext, ext., extension
//
//Area code: $1$2
//Exchange code: $3
//Station code: $4
//Extension / Extra characters: $5
//
//Separators are not accepted within the $3$4$5 portion of a vanity number.
//
//Examples:
// (540) 555-0123 ext.678 --> ($1$2) $3-$4 ext.$5
// 1-800-GOODFOOD --> 1-$1$2-$3$4$5
//10-digit phone number
/^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})$/;
//10-digit phone number, extension allowed
/^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/;
//7 or 10-digit phone number
/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})$/;
//7 or 10-digit phone number, extension allowed
/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/;
//10-digit phone number, vanity (alphanumeric) allowed
/^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9a-z]1[02-9a-z]|[2-9a-z][02-9a-z]1|[2-9a-z][02-9a-z]{2})([0-9a-z]{4})$/;
//10-digit phone number, vanity allowed, extra characters allowed at end
/^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9a-z]1[02-9a-z]|[2-9a-z][02-9a-z]1|[2-9a-z][02-9a-z]{2})([0-9a-z]{4})([0-9a-z]*)$/;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment