# Phone Number RegEX Question [SPOILER INTENDED BLURRED]

For the phone number regex challenge in the JavaScript projects, I tackled the problem by solving one phone number solution at a time, as soon as I saw the solutions I was amazed how much simpler it was. Is this because my regex knowledge is lacking, how would you guys recommend me getting a better grasp of regex?

Thanks so much.

``````  let regexHypen = /^(1|1 )(\d{3})-\d{3}-\d{4}\$|^(\d{3})-\d{3}-\d{4}\$/g // 555-555-5555
let regexParAndHypen = /^(1|1 )\(\d{3}\)\d{3}-\d{4}\$|^\(\d{3}\)\d{3}-\d{4}\$/g // (555)555-5555
let regexParAndHypenWithOneSpace = /^\(\d{3}\) \d{3}-\d{4}\$|^(1|1 )\(\d{3}\) \d{3}-\d{4}\$/g //(555) 555-5555
let regexWithSpace = /^\d{3} \d{3} \d{4}\$|^(1|1 )\d{3} \d{3} \d{4}\$/g //555 555 5555
let regexNoSpace = /^\d{10}\$|^(1|1 )\d{10}\$/g
//above is a regex for hypens, that would be a case where it starts with 1
// what we can do is do 6 seperate regex operators, and
// then if our orginal
if(str.match(regexHypen)){
console.log("regexHypen is true");
return true;
}
if(str.match(regexParAndHypen)){
console.log("regexParAndHypen is true");
return true;
}
if(str.match(regexParAndHypenWithOneSpace)){
console.log("regexParAndHypenWithOneSpace is true")
return true;
}
if(str.match(regexWithSpace)){
console.log("regexWithSpace is true");
return true;
}
if(str.match(regexNoSpace)){
console.log("regexNoSpace is true");
return true;
}
else {
return false;
}
}

telephoneCheck("1 555 555 5555");
``````

It’s hard to come up with elegant solutions without enough practice and repetition. Once you get enough experience, you will come up with solutions that you will like.

Try going through other resources on regex. Like this.

https://regexone.com/lesson/introduction_abcs

Will put that on my to-do list for sure, thanks so much

1 Like

Lmao, thats gold, thanks for that

understanding and practicing

Use for practice: https://regexr.com/

1 Like