Regex 如何在javascript中显示此正则表达式结果?
正则表达式并不是我的强项。我有一个用于验证国际电话号码的正则表达式。验证位对我有效,但我不明白如何获取正则表达式结果并使用它格式化数字。我的问题是如何从正则表达式中找出我可以用来显示的分组Regex 如何在javascript中显示此正则表达式结果?,regex,Regex,正则表达式并不是我的强项。我有一个用于验证国际电话号码的正则表达式。验证位对我有效,但我不明白如何获取正则表达式结果并使用它格式化数字。我的问题是如何从正则表达式中找出我可以用来显示的分组 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 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
}
正则表达式不用于格式化任何内容。它们只是告诉您正在验证的字符串是否遵守正则表达式的规则。例如,用户在表单中输入电话号码。如果他们输入到表单中的字符串与正则表达式不匹配,那么使用正则表达式检查字符串的表单验证将显示类似于“电话号码格式不正确”的内容。在简化了那堆乱七八糟的正则表达式之后:
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-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}?
)
$
可能有更好的方法,但这只是一个验证奇迹,即使在大多数情况下也不能正确地进行验证。因此,如果我使用正则表达式替换字符串,不是隐式格式化吗?您的答案没有回答OP的问题。您可以使用正则表达式“测试”字符串是否遵守正则表达式规定的规则。正则表达式用于获得真/假结果。它只是检查字符串是否遵循某种模式。看看这个链接。有关regex.OK的更深入的描述,我将看一看。我显然不知道我在说什么。@gsirianni你是对的,但是如果正则表达式是用正确的分组表示的,运行正则表达式查看模式是否匹配的结果可以提供信息,这些信息随后可用于创建原始字符串的格式。@gsirianni以下是有关分组和后续反向引用的详细信息,如果找到匹配,这些信息可用于格式化正是我的想法+1.那个正则表达式真是一团糟。@sln谢谢。我显然需要花时间在正则表达式上,因为现在我正在从网络上获取一些我不完全理解的东西!任何关于了解regex的最佳方式的建议都会受到赞赏。很棒的东西失败了。在处理正则表达式时,有没有我应该使用的工具?@keerz别忘了接受答案,或者在解决问题时发布自己的答案并接受它!