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

273
Vistas
Is there a way I can test a string using regex to be true only if ( & ) both happen at least once? The regex has to make sure both are used

In this case it's returning true but I need it to return false. I tried adding [()]{0,1} but I think it's only referring to just one"(or)" is there a way I can do [(&)]{0,1} so that "(" and ")" both have to happen at least once. Thank you

function telephoneCheck(str) {


const regex = /^[1]{0,1}[-\s]?[(]?\d{3}[-)\s]?[\s]?[\d]{3}[-\s]?[\d]{4}$/g
return regex.test(str)
}

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Given that your example involves telephone numbers, I'm going to assume that you are just looking for one pair of parentheses, and in one particular spot. You can use alternation to check for this:

/^1?[-\s]?(?:\(\d{3}\)|\d{3})[-\s]?\d{3}[-\s]?\d{4}$/g

The key is the non-capturing group starting with (?: which will pick either a sequence of three digits surrounded by parentheses or a sequence of three digits without parentheses. I also simplified the rest of your regex as you don't need to use so many brackets.

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