简体   繁体   English

用于根据状态前缀验证电话号码的复杂正则表达式

[英]Complex regular expression for validate phone number based on state prefix

I'm writing, or at least trying, a regular expression for validate Venezuela phone numbers in Javascript. 我正在编写或至少正在尝试使用正则表达式来验证Javascript中的委内瑞拉电话号码。 I made a basic expression: /^0[0-9]{10}$/ which allow numbers like for example: 我做了一个基本表达式: /^0[0-9]{10}$/ ,它允许使用数字,例如:

02129876543 (VALID)
02139876543 (INVALID)
02149876543 (INVALID)

But 2nd and 3th are not valid since the prefix values 213 and 214 are not valid. 但是第2个和第3个无效,因为前缀值213214无效。 I have a list of valid prefixes (taken from Wikipedia) and is this one: 我有一个有效前缀列表(取自Wikipedia),是这个列表:

248, 281, 282, 283, 235, 247, 278, 243, 244, 245, 246, 273, 278, 235, 285, 286, 288, 
241, 242, 243, 245, 249, 258, 287, 212, 259, 268, 269, 237, 235, 238, 246, 247, 251, 
252, 253, 271, 273, 274, 275, 212, 234, 239, 287, 291, 292, 295, 255, 256, 257, 293, 
294, 276, 277, 271, 272, 212, 251, 253, 254, 261, 262, 263, 264, 265, 266, 267, 271, 
275, 260, 270, 412, 414, 424, 416, 426

How I can build a regular expression for check validity of these prefixes? 如何建立正则表达式来检查这些前缀的有效性? A valid number should start always with zero (0) followed by any of the prefixes above, followed by seven digits, can any give me some help? 有效数字应始终以零(0)开头,后跟上述任何前缀,再后跟七个数字,有什么可以帮助我的吗?

您的正则表达式很长,要包含所有这些前缀,但是您可以在此交替模式下构建一些东西:

/^0(?:212|24[123589]|252|294)[0-9]{7}$/

Building on to @anubhava's answer, you would build it out for each. 以@anubhava的答案为基础,您将为每个答案建立答案。 The overall regex would look like this.. 整个正则表达式看起来像这样。

^0(?:2(?:12|3[45789]|[45][1-9]|6[0-9]|7[0-8]|8[1235678]|9[1-5])|4(?:1[246]|2[46]))\d{7}$

Live Demo 现场演示

And the complete regex would be, 完整的正则表达式就是

^0(?:2(?:12|4[1-9]|5[1-9]|6[0-9]|7[0-8]|8[1-35-8]|9[1-5]|3[45789])|4(?:1[246]|2[46]))\d{7}$

DEMO 演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM