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

175
Views
How do I make an query parameter optional in regex?

i have this regex:

^https?:path\?id=([a-zA-Z0-9._]+)&?.*&gl=([^&|\n|\t\s]+)&?.*$

the query parameter:

?id=([a-zA-Z0-9._]+)&?.*&gl=([^&|\n|\t\s]+)&?.*$

how do i make "gl" to be optional?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use

^https?:path\?id=([^&]+)(?:.*?(?:&gl=([^&\s]+).*)?)?$

See the regex demo.

Details:

  • ^ - start of string
  • http - a fixed http string
  • s? - an optional s char
  • :path\?id= - a fixed :path?id= string
  • ([^&]+) - Group 1: one or more chars other than a & char
  • (?:.*?(?:&gl=([^&\s]+).*)?)? - an optional sequence of
    • .*? - any zero or more chars other than line break chars as few as possible
    • (?:&gl=([^&\s]+).*)? - an optional sequence of
      • &gl= - a fixed string
      • ([^&\s]+) - Group 2: one or more chars other than whitespace and &
      • .* - any zero or more chars other than line break chars as many as possible
  • $ - end of string.
almost 4 years ago · Santiago Trujillo 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!