首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改此正则表达式,以验证电话号码而不使用国际化前缀?

如何更改此正则表达式,以验证电话号码而不使用国际化前缀?
EN

Stack Overflow用户
提问于 2021-02-05 19:16:00
回答 1查看 39关注 0票数 1

我不太喜欢REGEX,我正在找出一些问题来调整这个正则表达式来验证电话号码以适应我的用例。

我有一个REGEX验证电话号码,其国际前缀为:https://www.regextester.com/97440

代码语言:javascript
复制
(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})

它正确地验证字符串为:+39 3298494333,但它不验证表示没有国际前缀的数字的字符串,例如,该字符串与regex 3298494333不匹配

怎样才能接受没有前缀的电话号码呢?

EN

回答 1

Stack Overflow用户

发布于 2021-02-05 21:22:17

使用此修补程序:

代码语言:javascript
复制
(?:(?:\+\(?[0-9]{1,3}|\(?\b[0-9]{4})\)?)?\s*\)?[-\s.]?\(?[0-9]{1,3}\)?[-\s.]?[0-9]{3}[-\s.]?[0-9]{3,4}

证明

解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      \+                       '+'
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      [0-9]{1,3}               any character of: '0' to '9' (between
                               1 and 3 times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w)
                               and something that is not a word char
--------------------------------------------------------------------------------
      [0-9]{4}                 any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    \)?                      ')' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \(?                      '(' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [0-9]{1,3}               any character of: '0' to '9' (between 1
                           and 3 times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3}                 any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3,4}               any character of: '0' to '9' (between 3
                           and 4 times (matching the most amount
                           possible))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66069302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档