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

123
Views
JS regex match zero or more optional start with key words

Given string like keydown.capture.once.prevent.shift.control[arrowdown,arrowup]:silent I want to match shift.control[arrowdown,arrowup]:silent

And if the same keydown.capture.once.prevent.shift[arrowdown,arrowup]:silent then it should match shift[arrowdown,arrowup]:silent

Here the keywords shift | control are optional, ex: keydown.capture.once.prevent.[arrowdown,arrowup]:silent, matching string [arrowdown,arrowup]:silen

I wrote the below regex, it can only capture zero or one keyword, but the expected result is zero or all keywords matching into separate group

(shift|control)?\[(.*?)\]:silent, how can we capture all the keywords if they exist?

Additional notes: Order of keywords doesn't matter, ex it can be control.shift[]:silent

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

0

You may use any one of these regex solutions:

(?:(?:shift|control)\.?)*\[([^\]]*)\]:silent

This matches shift or control` optionally followed by a dit and repeats this non-capture group 0 or more times.

RegEx Demo

(?:shift(?:\.control)?|control(?:\.shift)?)?\[([^\]]*)\]:silent

This one matches shift.control OR control.shift in an optional non-capture group with the parts after dot optional in each alternation.

RegEx Demo 2

about 4 years ago · Juan Pablo Isaza Report

0

You could try this:

(?:(shift|control|shift\.control|control\.shift))?(\[.*\]):silent

Test and output: https://regex101.com/r/rMSd2l/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!