Topic describes

Their thinking

  • In this case, I use the regular method, by considering multiple cases, because all the test cases have been given, as long as all cases can be considered, through the regular expression can pass the test.
  • This question may not be the best solution. If readers have other better solutions, please leave comments.

The problem solving code

function telephoneCheck(str) {
    let reg = /^1\s? \(\d{3}\).? \d{3}.? \d{4}|^1\s\d{3}[\(|-]\d{3}[-]\d{4}|^\d{10}$|\d{3}\s\d{3}\s\d{4}|^\(\d{3}\)\d{3}-\d{4}|^\d{3}-\d{3}-\d{4}$/
    const bol = reg.test(str);
    return bol
}
Copy the code

CodeSandBox online demo

The title to reflect

Mastering regular expressions will greatly improve your development efficiency.