简体   繁体   中英

Regex to check url with only one occurrence of character “?” and that too after specific image formats

I want to place a validation to check image url so that i get only one occurrence of "?" and that too after specific image formats.

https://img1.com/1.jpeg: valid

https://img2.com/16.png?bg=0: valid

https://img3.com/15.jpg?bg=0&wmi=n : valid

https://img4.com/1?2.jpg?bg=0&wmi=n : invalid (two ? in url)

https://img5.com/12.jpg??bg=0&wmi=n : invalid (two ? in url)

https://img6.com/1.xyz: invalid (jpg|png|gif|jpeg only supported)

Also, image formats can be jpg|png|gif|jpeg.

I have tried using

\.(?i)(jpg|png|gif|jpeg)(?=\?|$) 

which works fine but cannot detect more than one occurence of "?"

If you just want to match with the valid urls than you can use expression below

(?i)/([^?]*?)\.(png|jpe?g|gif)\??(?!\?)

It matches all valid urls and doesn't match all invalid.

怎么样:

\.(?i)(jpg|png|gif|jpeg)(?:\?[^?]+)?$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM