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

138
Views
Using RegExp to test words for letter count

I've been trying to use RegExp in JS to test a string for a certain count of a substrings. I would like a purely RegExp approach so I can combine it with my other search criteria, but have not had any luck.

As a simple example I would like to test a word if it has exactly 2-3 as.

case test string expected result
1 cat false
2 shazam true
3 abracadabra false

Most of my guesses at regex fail case 3. Example: ^(?<!.*a.*)((.*a.*){2,3})(?!.*a.*)$

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Could use this regex.

With any other character, including whitespace.

^[^a]*(?:a[^a]*){2,3}$

or if using multi-lines and don't want to span.

^[^a\r\n]*(?:a[^a\r\n]*){2,3}$

^                  # Begin 
[^a]*              # optional not-a
(?:                # Grp
  a                  # single a
  [^a]*              # optional not-a
){2,3}             # Get 2 or 3 a only
$                  # End

https://regex101.com/r/O3SYKx/1

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!