Match all URLs exclude jpg,gif,png

﹥>﹥吖頭↗ 提交于 2020-01-10 06:00:51

问题


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  

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/11198001/match-all-urls-exclude-jpg-gif-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!