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

109
Vistas
Javascript matches less than n occurances even thought it's explicitly set tomatch n

Okay, so, I have this pattern

^(([0-9*\/]+)\s??){7}$

By default, it's supposed to match 7 segments that can have one of these sample forms:

[0/2, *, 2, /]

The problem is, this matches:

  • * * 0/2 * * - incorrect
  • * * * * * * * - correct
  • * 0/2 * * * 2 * - correct
  • * 0/2 0/2 - incorrect

In C# I would just use ++ on the first capture group and it would work, but Javascript has an outdated regex that's missing key functionalities.

Here's the playground to reproduce: https://regex101.com/r/Pk23dV/1

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

0

The (([0-9*\/]+)\s??){7}$ pattern is of ^((a)b?){x}$ type where the a part is obligatory and b part is optional. That is, the {7} quantifier in your regex means there must be at least seven occurrences of a char matched with the [0-9*\/] pattern, whether consecutive or sepearated with whitespace.

If you wanted to write an expression where \s is obligatory in between [0-9*\/]+ parts, you can use either

^[0-9*\/]+(?:\s[0-9*\/]+){6}$
^(?:([0-9*\/]+)(?:\s(?!$)|$)){7}$

See the regex demo 1 and regex demo 2.

Regex details

  • ^ - start of string
  • [0-9*\/]+(?:\s[0-9*\/]+){6} - one or more digits, / or * and then six occurrences of a whitespace and then one or more digits, / or *
  • (?:([0-9*\/]+)(?:\s(?!$)|$)){7} - seven occurrences of one or more digits, / or * followed with either a whitespace if not at the end of string or the end of string
  • $ - end of 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