Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda