简体   繁体   English

正则表达式-使用可选的国家/地区代码验证手机

[英]Regex - Validate Mobile Phone with Optional Country Code

I'm currently studying regular expression. 我目前正在研究正则表达式。 I didn't really know / understand how to write an expression with 2 condition. 我真的不知道/不知道如何编写带有2个条件的表达式。 Or do I have to write 2 regex for each case? 还是我必须为每种情况编写2个正则表达式?

What I am on is, 我在说的是

I have a txtMobile field in my html field 我的html字段中有一个txtMobile字段

<label for="txtMobile">Mobile:*</label>
<input id="txtMobile" type="text" name="txtMobile" size="22" maxlength="22">

In my javascript file I'm trying to create a expressions with the following rule 在我的javascript文件中,我尝试使用以下规则创建表达式

Format: (+##)###-###-####/#### extension is optional and it may start without country code. 格式:(+ ##)###-###-#### / ####扩展名是可选的,它可以不带国家/地区代码开始。

Is there any way to do in a single expression or do I have to type if conditions for each case in my javascript file? 是否可以在单个表达式中执行任何操作,或者如果我的javascript文件中每种情况的条件都必须键入?

Here is what I did /^([0-9]{3})\\-([0-9]{3})\\-([0-9]{4})\\/([0-9]{4})$/ 这是我所做的/^([0-9]{3})\\-([0-9]{3})\\-([0-9]{4})\\/([0-9]{4})$/

Make the groups optional by using ? 通过使用?将组设为可选 which means "zero or one of the preceding": 意思是“零或前述之一”:

/^([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$/

? is a modifier like * (zero or more of the preceding) and + (one or more of the preceding). 是修饰符,例如* (前面的零个或多个)和+ (前面的一个或多个)。

This makes both groups optional which means that numbers with all three components (country code, number, extension), numbers with either of the optional components (number + extension, country code + number), and numbers with neither of the optional components, will be accepted by the regex. 这使得这两个组都是可选的,这意味着具有所有三个组成部分(国家代码,号码,分机号)的数字,具有两个可选组成部分(数字+扩展名,国家/地区代码+数字)的数字以及没有任何可选组成部分的数字将被正则表达式接受。

EDIT 编辑

Your mistake in the regex in your comment is that the ? 您在评论中使用正则表达式的错误是? is after the escaped ) , which means zero or one of an actual ) and not of a group. 逸出 ) ,表示零或实际)之一,而不是一组。 What you need is: 您需要的是:

^(\(\+[0-9]{2}\))?([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$

Use ? 使用? to indicate an optional atom. 表示可选原子。

/^(\(+[0-9]{2}\))?...

Here I have made the preceding group optional, and notice how the parentheses group several atoms into a single atom. 在这里,我将前面的组设为可选,并注意到括号是如何将几个原子分组为一个原子。 The literal parentheses are escaped. 文字括号被转义。 The is the start of a regex that allows an optional country code following your format. 这是正则表达式的开始,它允许遵循您的格式的可选国家代码。 Allowing for an optional extension is left as an exercise to the reader. 允许可选的扩展留给读者练习。 :) :)

Like the other guys said ? 像其他家伙说的那样? matches 0 or 1 of the proceeding token. 匹配进程令牌的0或1。

/^(\(\+\d{1,3}\))?(\d{3}-){2}\d{4}(\/\d{3,4})?$/

Matches: 火柴:

999-999-9999
(+1)999-999-9999
(+11)999-999-9999
(+111)999-999-9999
999-999-9999/000
(+1)999-999-9999/000
(+11)999-999-9999/000
(+111)999-999-9999/000
999-999-9999/0000
(+1)999-999-9999/0000
(+11)999-999-9999/0000
(+111)999-999-9999/0000

I like to use this tool: http://gskinner.com/RegExr/ 我喜欢使用此工具: http : //gskinner.com/RegExr/

It implements Flash's version of RegEx, which is not perfectly identical to Javascript. 它实现了Flash的RegEx版本,该版本与Javascript并不完全相同。 But it's close enough for most work. 但这对于大多数工作来说已经足够接近了。 If someone else can suggest a JS RegEx tool, even better. 如果有人可以建议使用JS RegEx工具,那就更好了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式验证电话号码中的国家/地区代码 - How to make regex that validate country code in phone number 如何使用正则表达式使用国家/地区代码格式化手机号码 - How to format mobile phone number with country code using regex 移动电话号码的正则表达式使国别代码的选区为可选,破折号,点和空格为可选 - Regex for mobile number make parathesis as optional for country code and dashes, dots and spaces are optional 验证电话号码和国家/地区代码组合以验证Angular 4中的电话号码吗? - Validate Phone Number and Country Code combination for phone number validition in Angular 4? 为丹麦创建正则表达式并验证手机号码 - Create regex and validate mobile phone number for Denmark 带或不带国家代码的手机号码的正则表达式 - Regex for Mobile Number with or without Country Code 使用正则表达式验证javascript中手机号码的国家/地区代码 - Regular Expression to validate the Country code of mobile number in javascript 如何分别获取国家代码和电话号码并根据javascript中的国家代码验证ph号码的长度? - How to get country code and phone number separately and validate length of the ph number based on country code in javascript? 我需要一个正则表达式来验证带有或不带有国家代码的约旦号码 - I need a regex to validate a Jordanian number with or with out the country code ASP.NET Regex验证手机号码 - ASP.NET Regex to validate mobile phone number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM