如何在javascript中显示此正则表达式结果?

时间:2011-12-02 17:27:28

标签: regex

正则表达并不完全是我的强项。我得到了一个用于验证国际电话号码here的正则表达式。验证位对我有用,但我不明白如何获取正则表达式结果并使用它来格式化数字。我的问题是如何从正则表达式中找出我可以用来显示的分组?

var intl1RegexObj = /^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$/;


if (IntlRegexObj.test(businessPhoneValue)) 
{
    var formattedPhoneNumber = businessPhoneValue.replace(IntlRegexObj, "($1)");

    // display formatted result
}

3 个答案:

答案 0 :(得分:2)

简化了正则表达式的混乱之后:

if (subject.match(/^((?:\+)?[1-9]{1,2})?[\-\s.]?((?:\(\d{1,4}\))|\d{1,4})([\-\s.]?\d{1,12}){1,2}$/)) {
    // Successful match
}

现在只有3个捕获组。

第一个$1很简单,国家/地区代码可选+。

然后你有局部区号,基本上是1-4个带/不带括号的数字,可选择[-\s.]前缀。那是$2

最后,您有实际的电话号码,可以是1到24个号码,包括可选空格或点或负号[-\s.]

更详细的解释:

"
^              # Assert position at the beginning of the string
(              # Match the regular expression below and capture its match into backreference number 1
   (?:         # Match the regular expression below
      \+       # Match the character “+” literally
   )?          # Between zero and one times, as many times as possible, giving back as needed (greedy)
   [1-9]       # Match a single character in the range between “1” and “9”
      {1,2}    # Between one and 2 times, as many times as possible, giving back as needed (greedy)
)?             # Between zero and one times, as many times as possible, giving back as needed (greedy)
[-\s.]         # Match a single character present in the list below
               # The character “-”
               # A whitespace character (spaces, tabs, line breaks, etc.)
               # The character “.”
   ?           # Between zero and one times, as many times as possible, giving back as needed (greedy)
(              # Match the regular expression below and capture its match into backreference number 2
               # Match either the regular expression below (attempting the next alternative only if this one fails)
      (?:      # Match the regular expression below
         \(    # Match the character “(” literally
         \d    # Match a single digit 0..9
          {1,4}# Between one and 4 times, as many times as possible, giving back as needed (greedy)
         \)    # Match the character “)” literally
      )
   |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \d       # Match a single digit 0..9
         {1,4} # Between one and 4 times, as many times as possible, giving back as needed (greedy)
)
(              # Match the regular expression below and capture its match into backreference number 3
   [-\s.]      # Match a single character present in the list below
               # The character “-”
               # A whitespace character (spaces, tabs, line breaks, etc.)
               # The character “.”
      ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
   \d          # Match a single digit 0..9
      {1,12}   # Between one and 12 times, as many times as possible, giving back as needed (greedy)
){1,2}         # Between one and 2 times, as many times as possible, giving back as needed (greedy)
$              # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

答案 1 :(得分:1)

这个正则表达式是完全不合适的。当我转到您的链接时,即使是非匹配中列出的几个也会与此正则表达式匹配。正则表达式纯粹是可能性的重叠,通过恰好是捕获分组的分组的外观。任何解析数字真实部分的感觉都会被这个正则表达式遗忘。

扩展,它看起来像这样:

^
  (
     (\+)?
     [1-9]{1,2}
  )?
  ([-\s\.])?
  (
     (
       \(\d{1,4}\)
     )
   |
     \d{1,4}
  )
  (
     ([-\s\.])?
     [0-9]{1,12}
  ){1,2}
$

我甚至尝试将适当的捕获分组论证为其部分,遗憾的是它显示了问题。

^
 (?: \+ )?
 ( [1-9]{1,2} |)      # Capt Group 1, international code (or not)

 (?|                  # Branch Reset
     \( (\d{1,4}) \)     # Capure Group 2, area code
   |    (\d{1,4})
 )

 (?:[-\s.])?

 (                    # Capt Group 3, the rest ########-########
   [0-9]{1,12}
   [-\s.]?
   [0-9]{1,12}?
 )
$

可能会有一些更好的东西,但这只是一个验证奇迹,在大多数情况下甚至不能正常工作。

答案 2 :(得分:-1)

正则表达式不用于格式化任何内容。他们只是告诉您正在验证的字符串是否遵守正则表达式的规则。示例将是用户输入电话号码的形式。如果他们输入表单的字符串与正则表达式不匹配,那么使用正则表达式检查字符串的表单验证会说“电话号码格式不正确”。