Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!