Regex checking fails on hyphen symbol

Tell us what’s happening:

let skip talk about if else statement. I believe my regex is failing because I cannot catch a hyphen symbol by using ‘-?’. Any suggestion how I can improve this piece of regex (\s?|-?). Please.

  **Your code so far**

function telephoneCheck(str) {
const regex = /1?\s?\(?\d{3}\)?(\s?|\-?)\d{3}(\s?|\-?)\d{4}/;
if (str==str.match(regex)){
  return true;
}
// str.match(regex);
return false;
}

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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36

Challenge: Telephone Number Validator

Link to the challenge:

Actually it works. I forgot to add /g to the end of expression. Still cannot resolve issue with unbalanced parenthesis

That’s going to be hard to do since it is affecting the value returned from your function. I would read up on what value the match method returns.

1 Like

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