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

106
Vistas
Regex match any js number

So as an exercise I wanted to match any JS number. This is the one I could come up with:

/^(-?)(0|([1-9]\d*?|0)(\.\d+)?)$/

This however doesn't match the new syntax with underscore separators (1_2.3_4). I tried a couple of things but I couldn't come up with something that would work. How could I express all JS numbers in one regex?

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

0

how about this?

^(-?)(0|([1-9]*?(\_)?(\d)|0|)(\.\d+)?(\_)?(\d))$
about 4 years ago · Juan Pablo Isaza Denunciar

0

For the format in the question, you could use:

^-?\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?$

See a regex demo.

Or allowing only a single leading zero:

^-?(?:0|[1-9]\d*)(?:_\d+)*(?:\.\d+(?:_\d+)*)?$

The pattern matches:

  • ^ Start of string
  • -? Match an optional -
  • (?:0|[1-9]\d*) Match either 0 or 1-9 and optional digits
  • (?:_\d+)* Optionally repeat matching _ and 1+ digits
  • (?: Non capture group
    • \.\d+(?:_\d+)* Match . and 1+ digits and optionally repeat matching _ and 1+ digits
  • )? Close non capture group
  • $ End of string

See another regex demo.

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