gpt4 book ai didi

Problems using regex to validate an US phone number(使用正则表达式验证美国电话号码时出现的问题)

转载 作者:bug小助手 更新时间:2023-10-25 16:40:18 34 4
gpt4 key购买 nike



Return true if the passed string looks like a valid US phone number.

如果传递的字符串看起来像有效的美国电话号码,则返回TRUE。


The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the tests below for other variants):

用户可以选择任何方式填写表单域,只要其格式为有效的美国数字即可。以下是美国数字的有效格式示例(有关其他变体,请参阅下面的测试):



  • 555-555-5555

  • (555)555-5555

  • (555) 555-5555

  • 555 555 5555

  • 5555555555

  • 1 555 555 5555


For this challenge you will be presented with a string such as 800-692-7753 or 8oo-six427676;laskdjf. Your job is to validate or reject the US phone number based on any combination of the formats provided above. The area code is required. If the country code is provided, you must confirm that the country code is 1. Return true if the string is a valid US phone number; otherwise return false.

在此挑战中,您将看到一个字符串,如800-692-7753或800-six427676;laskdjf。您的工作是根据上面提供的格式的任意组合来验证或拒绝美国电话号码。区号是必填项。如果提供了国家代码,则必须确认国家代码为1。如果字符串是有效的美国电话号码,则返回TRUE;否则返回FALSE。


This is my code:

这是我的代码:


   function telephoneCheck(str) {
let check = /^1*[\(|-| ]*(\d{3})[\)|-| ]*(\d{3})[- ]*(\d{4})$/
return check.test(str);
}

console.log(telephoneCheck("1 555-555-5555"));


This are the tests that I'm failing:

以下是我没能通过的测试:



  • telephoneCheck("1 555-555-5555") should return true

  • telephoneCheck("555-555-5555") should return true.

  • telephoneCheck("1 555)555-5555") should return false.


I understand why I'm failing the last test, I'm having problems with the lookahead conditional, when i manage to make it work i screwed up other tests that were correct in the beginning.
But I honestly don't get why I'm failing the first two tests.

我明白为什么我不能通过上一次测试,我在前瞻条件方面有问题,当我设法让它工作时,我搞砸了其他一开始正确的测试。但我真的不明白为什么我前两次测试都不及格。


This are all the other tests:

以下是所有其他测试:


Passed:telephoneCheck("555-555-5555") should return a boolean.

Failed:telephoneCheck("1 555-555-5555") should return true.

Passed:telephoneCheck("1 (555) 555-5555") should return true.

Passed:telephoneCheck("5555555555") should return true.

Failed:telephoneCheck("555-555-5555") should return true.

Passed:telephoneCheck("(555)555-5555") should return true.

Passed:telephoneCheck("1(555)555-5555") should return true.

Passed:telephoneCheck("555-5555") should return false.

Passed:telephoneCheck("5555555") should return false.

Failed:telephoneCheck("1 555)555-5555") should return false.

Passed:telephoneCheck("1 555 555 5555") should return true.

Passed:telephoneCheck("1 456 789 4444") should return true.

Passed:telephoneCheck("123**&!!asdf#") should return false.

Passed:telephoneCheck("55555555") should return false.

Passed:telephoneCheck("(6054756961)") should return false.

Passed:telephoneCheck("2 (757) 622-7382") should return false.

Passed:telephoneCheck("0 (757) 622-7382") should return false.

Passed:telephoneCheck("-1 (757) 622-7382") should return false.

Passed:telephoneCheck("2 757 622-7382") should return false.

Passed:telephoneCheck("10 (757) 622-7382") should return false.

Passed:telephoneCheck("27576227382") should return false.

Passed:telephoneCheck("(275)76227382") should return false.

Passed:telephoneCheck("2(757)6227382") should return false.

Passed:telephoneCheck("2(757)622-7382") should return false.

Passed:telephoneCheck("555)-555-5555") should return false.

Passed:telephoneCheck("(555-555-5555") should return false.

Passed:telephoneCheck("(555)5(55?)-5555") should return false.

Passed:telephoneCheck("55 55-55-555-5") should return false.

Passed:telephoneCheck("11 555-555-5555") should return false.

Thanks in advance!

提前谢谢!


更多回答

Drop your expression at regexr.com and carefully read through the English explanation of what the expression matches.

在regexr.com上放下你的表情,仔细阅读该表情与之匹配的英文解释。

优秀答案推荐

Regex:

Regex:


/^1?[- ]*(\d{3}|\(\d{3}\))[- ]*(\d{3})[- ]*(\d{4})$/

Demo:

演示:


https://regex101.com/r/cO5LK0/1

Https://regex101.com/r/cO5LK0/1


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com