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

205
Vistas
Regex length check account for optional group
export const validUKPhone = /^(\+)?(44)?(\s*\d){9,11}$/;

I currently have the following RegEx for a telephone number, that is trying to validate the length of a number.

Now the fun bit of this, is that there are 3 optional characters at the start of the pattern. +44 (potentially).

My question is how to write my regex to take account for this group, and only count the length of the 'main body' of the number. If the +44 exists, the length pattern would be {12,14} otherwise {9,11}

e.g. The following test fails.

  expect(regex.test('+440798444')).toBeFalsy();
  expect(regex.test('+440798444457')).toBeTruthy(); 

(10 characters currently because of the +44 but returns true)

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

0

You can use

/^(?:\+?44|(?!44))(?:\s*\d){9,11}$/

See the regex demo. Details:

  • ^ - start of string
  • (?:\+?44|(?!44)) - a non-capturing group matching:
    • \+?44 - an optional + and then 44
    • | - or
    • (?!44) - (a negative lookahead that matches) a location that is not immediately followed with 44 (add \s* if you do not want to match the number even if there are whitespaces before/inside 44)
  • (?:\s*\d){9,11} - nine to eleven occurrences of zero or more whitespaces and then a digit
  • $ - end of string.
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