How to make an query parameter optional in regex?

by giovanny.lueilwitz , in category: Third Party Scripts , 18 days ago

How to make an query parameter optional in regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 18 days ago

@giovanny.lueilwitz 

To make a query parameter optional in a regular expression, you can use the question mark (?) symbol after the parameter you want to make optional. Here is an example:


Let's say you have a URL pattern that includes a query parameter "page":

1
^https://example.com/path?page=d+$


To make the "page" parameter optional, you can add the question mark after the parameter:

1
^https://example.com/path(?page=d+)?$


This will match URLs with or without the "page" parameter attached.