Tele Number Validator

Tele Number Validator
0

#1

Hello everybody , i need some help to pass the telephoneCheck test .
now everything is going alright except for this part of the regex:


(?:\)(?<=\(\d{3}))




it’s a positive lookbehind (after a research i found out that it’s not supported here ) , so is there an alternative for it ? that’s the full code :smiley: thanks for your help .





function telephoneCheck(str) {
  
  // Good luck!
  return str.match(/(?:1 )?(?:\((?=\d{3}\)))\d{3}(?:\)(?<=\(\d{3}))[- ]?\d{3}[- ]?\d{4}/g);
}

console.log(telephoneCheck("1 (555)-555-5555"));

#2

Positive and negative lookaheads are supported in JavaScript regular expressions. The syntax is:

x(?=y)

The above matches ‘x’ only if ‘x’ is followed by ‘y’


#3

you see … the problem is not in the lookahead it’s in the lookbehind :smiley:


#4

Sorry about that. I misread your original post. I should have slept longer this morning.


#5

well i guess i found the solution LOL , i don’t even need a lookbehind regex , all i had to do is match the part where the problem is only if it is formatted like this :
(xxx) or xxx where x is a number between 0 to 9 .
here’s the full code :



function telephoneCheck(str) {
  
  // Good luck!
  return /^1? ?(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}$/.test(str);
}

console.log(telephoneCheck("1 (555)-555-5555"));

#6

Glad you figured it out. In the future, it is not necessary to post your solution and we would prefer you did not anyway, to keep from spoiling solutions for others.

Thank you.


#7

alright , sorry about that , i got carried by the joy of finding the answer :smiley: