Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda