正则表达式使电话号码与国家/地区代码匹配 [英] Regular expression to match phone numbers with country codes

查看:38
本文介绍了正则表达式使电话号码与国家/地区代码匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来匹配电话号码的正则表达式,例如:

This is the regular expression I use to match phone numbers like:

00 00 00 00 00
00 00 0 00 00 00 00
+00 0 00 00 00 00

(\d{2}\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2})\s+(\d{2}\s+\d{2}\s+\d\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2})\s+(+\d{2}\s+\d\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2})

我试图将其包含到我的javascript中,但实际上并不能正常工作

I have tried to include it into my javascript but It's not really working

if(document.maj_profil.phone.value.search(/^\(\d{2}\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2}\)\s+\(\d{2}\s+\d{2}\s+\d\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2}\)\s+\(+\d{2}\s+\d\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2}\)/) == -1)
    {
    displayDialog('wrong phone format')
        }

推荐答案

  1. 您列出的替代格式如下:

  1. Where you have your alternative formats listed like:

(...)\s+(...)\s+(...)

更改为使用 | (OR)运算符:

Change that to use the | (OR) operator:

(...)|(...)|(...)

  • 不要逃避括号. \( \)应该只是().

    在第三组中,开头的 + 应该以反斜杠转义:

    In your third group the + at the beginning should be escaped with a backslash:

    (\+\d{2}\s+\d\s+\d{2}\s+\d{2}\s+\d{2}\s+\d{2})
    

  • 这篇关于正则表达式使电话号码与国家/地区代码匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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