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

266
Vistas
How can I allow only "-" at first character in regex?

I'm using React-native I made a regex that accepts only numbers. Here, I want to allow one minus at the beginning only. That is, I want to create a regular expression that allows "-" only once, but can be used or not. So I applied my code, but it doesn't work. How do I fix my regular expression?

The restriction is that the minus must not be placed in the middle of or after the number, only at the beginning of the number, and the minus can be used or not.

       let minCheck = !/^[0-9]{1,4}$/g.test("-1")

i tried

       let minCheck = /-?!/^[0-9]{1,4}$/g.test(valueRange.minValue)

but it doesn't work

How can i fix my code?

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

0

Hopefully the following will fix your issue(in case you want to make minus sign compulosry):

/(-)([0-9]{1,4})/

In case you want to make minus sign optional, please try this:

/([-0-9]{1,4})/

If either of these don't work for you, please ask and I'll make one accordingly for you.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use below regEx:

Exp 1: /^(-)?\d+$/g.test("-213213")

Exp 2: /^(0|(-)?[1-9]\d*)$/g.test("12321")

  • "-" with ? makes it non-mandatory
  • d+ expects more than 1 digits
  • Exp 1 allows start with 0 for ex: "-0" or "0123" will also be true. If you want to restrict that. Use Exp 2
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