简体   繁体   中英

Match all URLs exclude jpg,gif,png

I want to match all URLS but exclude image urls from beeing matched with that regex: jpe?g|gif|png .

\\b(?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*A-Z0-9+&@#/%=~_|$

The problem is that the part with the exclude is not working like this: (?!jpe?g|gif|png)

Does anyone have a solution for it?

Example:

not Matchen:

http://example.com/example.jpg
http://example.com/example231/example.gif

Match:

http://example.com/example.html
http://example.com/example/?id=4331
http://example.com/example/example/ex_ample/ex-ample/?id=4331  

Just start your regex with (?!.*(?:\\.jpe?g|\\.gif|\\.png)$) ,

so if your current regex is \\b(?:https?|ftp|file)://... , then merge it to

(?!.*(?:\\.jpe?g|\\.gif|\\.png)$)\\b(?:https?|ftp|file)://...

Read also PHP URL validation

I was working on this problem for a recent duplicate question that got closed, so I'll post it here:

((?!.*(png|jpg|gif)(?!.))(?:https?|file|ftp):\\/\\/.*.\\.(?:com|ca|net|museum|org|co\\.uk))

Feel free to add more protocals if appropriate, more image extensions if appropriate, and more top level domains if appropriate.

Tested on regex101

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