简体   繁体   中英

Simple regular expression modification to match only images

How can I edit the following regex

/(?<=src=")(.*?)(?=")/ui

to get only the matches that ends to jpeg, png, gif like the one below?

!http://.+\.(?:jpe?g|png|gif)!Ui

Thank you

/(?<=src=")([^"]+\.(jpe?g|png|gif))(?=")/ui

Replace the.* in the middle ( it represents file name ) - so this will match only if file name ends with jpg, jpeg, png, gif

EDIT:

Solutin with query string is:

/(?<=src=")([^"]+\.(jpe?g|png|gif))(\?[^"]*)?(?=")/ui

And i replaced. with [^"], because double quote is invalid in URI ( and not used often ) - or you can use this ([^"]|(?<=\)|") for escaped double quote

And in place of the catch-all (.*?) use ([^"]+) preferrably.

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