如何使用正则表达式验证器来获取电话号码,如999-999-9999 [英] How to use regex validator for phone numbers like 999-999-9999

查看:135
本文介绍了如何使用正则表达式验证器来获取电话号码,如999-999-9999的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为这种格式的电话号码制作一个正则表达式模式:999-999-9999

I'm trying to make a regex pattern for phone numbers like this format : 999-999-9999

所以我尝试了这个表达式:

So I tried this expression :

\+\d(-\d{3}){2}-\d{4} 

但格式适用于以下数字:+ 1-999-999-9999

But the format works on numbers like : +1-999-999-9999

如果有人帮我修改模式以使其与格式999-999-9999匹配,我将不胜感激,这意味着我不希望用户在字符串的第一个区域输入+号

I would appreciate if someone help me modifying the pattern to make it match the format 999-999-9999 , which means I don't want the user to enter the + sign at first of the string

谢谢

推荐答案

表达式 \ + \ d(-\d {3}){2} -\d {4} 您已尝试明确说:

The expression \+\d(-\d{3}){2}-\d{4} you have tried explicitly says:


A + 后跟一个数字后跟一个连字符和3位数(两次)后跟4位数。

A + followed by a single digit followed by a hyphen and 3 digits (two times) followed by 4 digits.

如果您不想要 + 和第一个数字,请不要在正则表达式中包含这些数字。

If you don't want the + and the first single digit, don't include those in your regular expression.

此表达式 \d {3} -\d {3} -\d {4} 说:


3位数后跟一个连字符后跟3位数后跟一个连字符后跟4位数。

3 digits followed by a hyphen followed by 3 digits followed by a hyphen followed by 4 digits.

这篇关于如何使用正则表达式验证器来获取电话号码,如999-999-9999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆