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

132
Views
Regex to catch all cases

I have the following stringified javascript-snippet that I would like to capture each case using regex

// case 1
let something = 1
something++

// case 2
const somethingElse = 2
const x = somethingElse + 10 // whatever

// case 3
enter code here

Cases keep going infinitely.. so I am looking for a regex that captures each case with all belonging code up until the next case is reached.

example of the first match should be

// case 1
let something = 1
something++

The best I could get is the following, but it always disregard the last case

([\/]{2}[\s]*?case[\s\S]+?(?=[\/]{2}))

Obviously, the Lookahead is the reason, I had other forms of regex but couldn't get a bulletproof regex..

Note: all cases should be identified consistently. The example above is using a simple pattern case [digit]

Any help is appreciated!

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

0

You can use a simple alternation in your look ahead:

/([\/]{2}[\s]*?case[\s\S]+?(?=[\/]{2}|$))/gi

I just added |$ in the look ahead, which means: OR end of text

So it will look ahead for / or end of text.

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!