Regular Expressions 101

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
^(?:(?<scheme>[^:\/?#]+):)?(?:\/\/(?<authority>[^\/?#]*))?(?<path>[^?#]*\/)?(?<file>[^?#]*\.(?<extension>[Jj][Pp][Ee]?[Gg]|[Pp][Nn][Gg]|[Gg][Ii][Ff]))(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?$
/
gm

Description

Adaptation of RFC-3986 uri validation - https://tools.ietf.org/html/rfc3986

Validates that URL contains an image (jpg/jpeg/png/gif) reference whilst also allowing for queries Key parts of URL are captured in named groups The URL portion is also optional, so will validate just an image filename if required

Submitted by Norris - 6 years ago