Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
I can't seem to figure out the regular expression

I have the following code. I am trying to have the regular express test the phone number for validation. With the current argument the function should return positive, but it doesn't and I can't figure out why.

function telephoneCheck(str) {

  let reg = /^[\d]{0,1}[\w]{0,1}[(]{0,1}[\d]{3}[-)\w]{0,2}[\d]{3}[-\w]{0,1}[\d]/;
  return reg.test(str);
  
}

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

Can anyone see what I am missing?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

First, replace all the \w (Matches any letter, digit or underscore) with \s (Matches any space, tab or newline character). I believe you don't won't letter in phone number. Second, you need to add a quantifier {0,4} to the end of your Regex, just like you already did in other positions of the Regex.

So the final Regex will be ^[\d]{0,1}[\s]{0,1}[(]{0,1}[\d]{3}[-)\s]{0,2}[\d]{3}[-\s]{0,1}[\d]{0,4}

about 4 years ago · Juan Pablo Isaza Relatório

0

Because your regex is nonsense.

  1. No need for [] for single group ([\d]{0,1} can be just \d?
  2. \w does not match spaces, just [a-z0-9] in general case
  3. You match starting ( but ending ) can be followed by - or any \w

function telephoneCheck(str) {

  let reg = /^\d?\s?\(?[\d-]+\)?\s?\d+/;
  return reg.test(str);
  
}

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

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. You need to replace \w with \s for whitespace
  2. You need to escape parenthesis \( and \)
  3. You need to change [-)\w]{0,2} to [-\)]{0,1}[\s]{0,1} unless you want the unorthodox 1 (555)-555-5555 to be true.
  4. The final [\d] should be [\d]{4} since you want exactly 4 digits at the end.
  5. You can replace the {0,1} with ? when evaluating a single character that is optional.
  6. You don't need brackets around single characters.

Here's the resulting code:

function telephoneCheck(str) {

  let reg = /^\d?\s?\(?\d{3}[-\)]{0,1}\s?\d{3}[-\s]?\d{4}/;
  return reg.test(str);
  
}

console.log(telephoneCheck("1 (555)555-5555")); // true
console.log(telephoneCheck("1 (555)5555555"));  // true
console.log(telephoneCheck("(555)555-5555")); //true
console.log(telephoneCheck("15555555555")); //true
console.log(telephoneCheck("1 (555)555- 5555")); // false
console.log(telephoneCheck("1 (555)-555-5555")); // false

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda