Moon
Moon

Reputation: 890

Regex rules for optional url parameter

How to write the regrex rules for the specific given url parameter?

For exmaple, I have URL below:

  1. http://localhost:5008/api/products/productsSubscribeStatus?companyId=14
  2. http://localhost:5008/api/products/productsSubscribeStatus
  3. http://localhost:5008/api/products/productsSubscribeStatus?ccc=14

I want to a rule only match "products/productsSubscribeStatus" and only with the parameter "companyID=xx", but does not pass the url with the other parameters like the third one.

I write the regrex: \/products\/productsSubscribeStatus(\?companyId=\d+$)?, but it cannot guarantee the parameter is companyId only.

Upvotes: 0

Views: 401

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

I would just make the entire expected query parameter optional:

^https?://.+/products/productsSubscribeStatus(?:\?companyId=\w+)?$

Demo

Upvotes: 1

Related Questions