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

437
Vistas
Regex: ONly Allow Upper case AND specific numbers

I'm trying to create regex and test in javascript.

RULE: The text should only be 8 characters. Only contain Upper case and numbers 1-5 No lower case or special characters or 6-7 numbers.

example:

12345678 -- false
a2345678 -- false
aabbccdd -- false
AABB33DD -- true // contains BOTH uppercase and 
                    numbers between 1-5 and nothing else
AABB88DD -- false 
AABBCCDD -- false
AABB3.DD -- false 

CODE:

 var pattern = new RegExp("^(?=.*[1-5])(?=.*[A-Z]).+$");
 pattern.test(code)

I'm not able to crate the right regex. Can anyone please help?

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

0

Use

^(?=.*[1-5])(?=.*[A-Z])[A-Z1-5]{8}$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [1-5]                    any character of: '1' to '5'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [A-Z1-5]{8}              any character of: 'A' to 'Z', '1' to '5'
                           (8 times)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           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