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

235
Vistas
What is regex for alphanumeric with no spaces?

I need to match this like:

1. 1234           true
2. 1234 5678      false
3. 1234x          true
4. x1234          true
5. abcd           false
6. abcd 1234      false

so I need to match just a string with numbers only or numbers and characters only and with no spaces (single word). This doesn't really work:

/([0-9])\w+/g
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your pattern ([0-9])\w+ is unanchored, so it can have partial matches.

It also has to start with a digit, and at least 1 word character, have the string to be at least 2 characters long.


You can use anchors, and make sure to match a single digit. For a match only, you can omit the capture group:

^[^\W\d]*\d\w*$
  • ^ Start of string
  • [^\W\d]* Optionally match any word character except a digit
  • \d Match a single digit
  • \w* Match optional word characters
  • $ End of string

Regex demo

Note that \w can also match _

about 4 years ago · Juan Pablo Isaza Denunciar

0

Match the entire string and require a at least one digit:

/^[a-z]*[0-9][a-z0-9]*$/i

See regex proof. Important: i flag (case insensitive).

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-z]*                   any character of: 'a' to 'z', 'A' to 'Z' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  [a-z0-9]*                any character of: 'a' to 'z', 'A' to 'Z', '0' to '9'
                           (0 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        the end of the string
about 4 years ago · Juan Pablo Isaza Denunciar

0

const strings = ['1234','12x3', '12 X'];
console.log(strings.filter(string=> string.match(/^[0-9a-z]+$/i)));

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