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

147
Vistas
Regex for a single range or comma separated string of numbers

I have a text field that I want to only accept a single range of numbers (eg. "1000-3000") OR a comma-separated list of numbers (eg. "1003", "1004", "2000") but not both. Some examples of expected results:

1000 (valid)
1000, 2000 (valid)
1000 ,2000 (valid)
1000, 2000, (valid)
,1000, 2000 (invalid)
1000,1000 (valid)
1000-2000, 3000-3500 (invalid).
1000-2000, 3000 (invalid).
1000 - 2000 (valid)
1000-2000 (valid)
,1000 (invalid)

This is what I have which doesn't work fully:

let input = "1000, 5000-6000"
input.match(/^(([0-9\s](,)?(-)?)*)+$/); // returns a match

input = "1000 - 3 000"  
input.match (/^(([0-9\s](,)?(-)?)*)+$/)// returns a match (space in second num)

let input = "5000-"
input.match(/^(([0-9\s](,)?(-)?)*)+$/); // returns a match

My version might fail in other ways I haven't found yet, but those 3 examples of matches shouldn't work. I'm using javascript/react.

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

0

You may use the following regex pattern, which matches a number either followed by a hyphen and another number (single range), or by comma and and another number, any number of times (CSV list).

^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*\s*,?)$

Sample script:

var inputs = ["1000", "1000, 2000", "1000 ,2000", "1000, 2000,", "1000,1000", "1000 - 2000", "1000-2000", ",1000, 2000", "1000-2000, 3000-3500", "1000-2000, 3000", ",1000"];
inputs.forEach(x => console.log(x + " => " + /^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*\s*,?)$/.test(x)));

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