How to make a more efficient solution (Regex to find mistakes in parenthesis)

I´m doing the challenge https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator and I got 3 test left to pass, with 1 I need to change somthing in my code but with the other two:

telephoneCheck(“1 555)555-5555”)
telephoneCheck(“555)-555-5555”)

It is just can be solved by checking if there are parenthesis, they are closed (so to test there aren´t only one “(” or only one ") and not it´s correspondent too )

For this I made this which works but it´s a little verbose, right:

let leftpar = /\)/
  let rightpar = /\(/

  // check if there are missing closing parenthesis
  if (str.match(leftpar) || str.match(rightpar)){
    if (str.match(leftpar)){
      if (str.match(rightpar)){
        1==1 //do nothing
      }
      else{
        return false
      }
    }
    else if (str.match(rightpar)){
      if (str.match(leftpar)){
        1==1 // do nothing 
      }
      else{
        return false
      }
    }
  }

I was wondering there could be an approach less verbose.(but I don´t want to look at solutions since I have left another test to pass which forces me to change somethings in my main code)

I could remember the time I was going through this challenge. Solved it with help from regular expressions. My suggestion is to spend a little time learning nuts and bolts of regular expressions. It’ll help you in the long run. As a resource, there is plenty of online regex previewers. Have fun :slight_smile:

1 Like

This regex will match three digits in parenthesis:

/\(\d{3}\)/g
1 Like

this may help you