Phone number Validator. Not able to pass 3 tests. Please help

Main thing is I don’t know how to use if else in regex like for example if ( this bracket exists then this ) bracket should also exist otherwise ) should not exist
I looked around in the internet and tried different patterns like these

(?(conditional-pattern)yes-pattern|no-pattern)
((?=conditional-pattern)yes-pattern|(?!conditional-pattern)no-pattern)

But none of them worked. I mean the second one worked but it failed other 3 tests which I had passed originally. I went through the regex multiple times but could not figure out what I am doing wrong. I can forcefully pass the test counting number of brackets and if it’s equal to 2 then return true if not then false but I feel like that would not be the right way as there could be two ( ( brackets and the tests would still pass. Can someone suggest/give hints or teach me Proper way to use if…then…else in regex in javascript

  **My code so far**

function telephoneCheck(str) {
let newStr=str.split("");
newStr=newStr.filter(a=>/\d/.test(a));
//regEx2 is just a dummy regex for testing if...then...else
let regEx2=/^(?!-)1*\s*?((?=\()\d{3}\)|(?!\()\d{3})-?\s?\d{3}\s?-?\d{4}(?!(\))$)/;
//regEx is my original regEx which passed all tests except three the ones where only three brackets are present
let regEx=/^(?!-)1*\s*?\(?\d{3}\)?-?\s?\d{3}\s?-?\d{4}(?!(\))$)/;
if (regEx2.test(str) && (newStr.length===10 || (newStr.length===11) && newStr[0]==='1')){return true;}

return false;
}

console.log(telephoneCheck("1 (555) 555-5555"));
//((?=conditional-pattern)yes-pattern|(?!conditional-pattern)no-pattern)
//((?=\())\d{3}\)|(?!\()\d{3})
//\(?\d{3}\)?
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.177

Challenge: Telephone Number Validator

Link to the challenge:

I don’t think RegEx supports if-then-else. But why bother?
JS supports if-then-else, so you can wrap that around the different conditions.

RegEx itself can combine conditions via or → and that’s basically all you need.

The only way to do if-then-else in RegEX I can think of is basically:
You got if condition1 then condition2 else condition 3.
In pure RegEx thats "condition1 and condition two" | "not-condition1 and condition3"

It’s the same as instead of using if-then-else, you just have a list of if-then conditions which either are designed so that only one can be true OR with keywords to stop the conditions checks (like return true)

I passed it using a simple OR statement, I don’t know why I didn’t think of it :slightly_smiling_face:

(\(\d{3}\)|\d{3})

If there is bracket then there should be three digits then another brackets. If there is no brackets then there should just be three digits.

1 Like

If you want a regex only solution, the below was what I had when solving this challenge:

const checker = RegExp(
    "^(?:1\\s?)?" //May start with "1" followed by a space.
    + "(?:\\d{3}|[(]\\d{3}[)])"
    + "[-\\s]?" //May exist a "-" or a space.
    + "\\d{3}" //Match 3 digits.
    + "[-\\s]?" //May exist a "-" or a space.
    + "\\d{4}$" //End with 4 digits.
    , "ig"); //Flags shouldn't be required.

I used the RegExp() constructor (where \ also needs to be escaped) so that I could narrate sections of the regex for easier understanding.

PS: Don’t stress about regex. Super complex regex isn’t a good thing anyway.

2 Likes

I wonder how this covers that it may not start with anything other than a 1 that is, it should not start with a 2?

To try using regex only, I tried to add this to the beginning of my but it failed two tests

(?:1\s?)?
telephoneCheck("27576227382")
telephoneCheck("(275)76227382")

My regEx

  let regEx = /^(?:1\s?)?(\(\d{3}\)|\d{3})-?\s?\d{3}\s?-?\d{4}(?!(\))$)/;

(?!(\))$)
This is causing problem

1 Like

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