I’m super close. My regex passes all but 2 test cases.
"1 555 555 5555"
“1 456 789 4444”
Here’s my regex
/^(+|1\s?)?(([0-9]{3})\s?|[0-9]{3}-?)[0-9]{3}-?[0-9]{4}$/
Can anyone tell me what I’m missing to satisfy these test cases?
I’m super close. My regex passes all but 2 test cases.
"1 555 555 5555"
“1 456 789 4444”
Here’s my regex
/^(+|1\s?)?(([0-9]{3})\s?|[0-9]{3}-?)[0-9]{3}-?[0-9]{4}$/
Can anyone tell me what I’m missing to satisfy these test cases?
Your regex accepts a one-or-zero -
s between the last two number sets (three digits followed by four digits), but those two examples have a space instead of a -
.
I think you also need to escape your +
in the first group.
Thank you for the suggestion! I have updated my regex to what I thought would satisfy based on your comment, but still no luck.
/^(\+|1\s?)?(\([0-9]{3}\)\s?|[0-9]{3}\-?)([0-9]{3}\-?|[0-9]{3}\s?)[0-9]{4}$/
Wouldn’t my addition allow for a “-” or a " " between the last 2 sets of numbers?
Now the problem is with your second capture group. It allows for either (ddd)
followed by one or zero spaces or ddd
followed by one or zero -
s (where d
is a digit). It does not allow for (ddd)
followed by a space.
Link to your regex in a regex101 test.
Got it! Thank you so much for your help, and showing me the online regex tester!
/^(\+|1\s?)?(\([0-9]{3}\)\s?|[0-9]{3}\-?|[0-9]{3}\s?)([0-9]{3}\-?|[0-9]{3}\s?)[0-9]{4}$/