匹配所有网址,不包括jpg,gif,png


Match all URLs exclude jpg,gif,png

我想匹配所有网址,但从与该正则表达式匹配的比方中排除图像网址:jpe?g|gif|png .

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

问题是带有排除的部分不是这样工作的:(?!JPE?g|gif|png)

有人有解决方案吗?

例:

不是马仙:

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

火柴:

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

只需用 (?!.*(?:'.jpe?g|'.gif|'.png)$) 开始正则表达式,

因此,如果您当前的正则表达式'b(?:https?|ftp|file)://...,则将其合并到

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

另请阅读 PHP URL 验证

我正在为最近关闭的重复问题解决这个问题,所以我将在此处发布:

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

如果适用,您可以随意添加更多协议,如果适用,可以添加更多

图像扩展,如果合适,还可以添加更多顶级域。

在正则表达式101上测试