What's a regex to match a 10-digit US phone number?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Notifications
A regex to match a 10-digit US phone number (in the format
XXX-XXX-XXXX
orXXXXXXXXXX
) would look like this:Explanation:
^\d{3}-\d{3}-\d{4}$
: Matches a phone number in theXXX-XXX-XXXX
format.^\d{10}$
: Matches a phone number without dashes (e.g.,XXXXXXXXXX
).\d{3}
and\d{4}
: Match exactly 3 or 4 digits respectively.This regex allows both formats for a 10-digit phone number.