Regular Expression not Passing Only Two Tests

Tell us what’s happening:
I’m having trouble getting the strings "1 555)555-5555" and "(555-555-5555" to return false. The issue is with the single parenthesis either to the left or right of the area code. I’ve spent a few hours researching on how to change my regex. After reading through the MDN cheatsheet for regular expressions as well as Stack Overflow, I’ve tried using a lookahead assertion around the area code to pass the parenthesis only if its matching closer/opener is included, but that just made more of the tests not pass.

Any tips or hints to help point me in the right direction are greatly appreciated.

  **Your code so far**

function telephoneCheck(str) {
let regExp = /^1*\s*\(*\d{3}-*\s*\)*\s*\d{3}-*\s*\d{4}$/;
if (str.match(regExp)) {
  return true;
} else {
  return false;
}
}

console.log(telephoneCheck("(555-555-5555"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0

Challenge: Telephone Number Validator

Link to the challenge:

The first step is to understand why something is matching your pattern when it should not be. Do you understand why 1 555)555-5555 is matching when it shouldn’t be? I would suggest you concentrate on just this sub problem for a bit. It seems that there are two ways you can represent an area code here. Do you know of a way you can match for either one pattern OR another pattern?

1 Like

Solved it. Yes, I understood why the strings were matching when they shouldn’t have been. Turns out the part that was tripping me up was where to place parentheses along with OR |. I experimented with it prior to posting here, but you helped solidify it for me. Thank you for your help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.