正则表达式匹配不以jpg或png结尾的URL

时间:2014-08-12 21:37:28

标签: regex

所以这是我到目前为止所得到的:

(?<=\s|\b)https?://[^ ]+(?<!jpe?g|png|bmp|gif)(?=\s|\b)

问题是,它与此匹配:

http://imgur.com/test/img.jpg(直到img)。我希望它不返回任何匹配。

基本上,这些应该匹配:

http://test.com/index.html
https://anything.net/this
check out this link: https://anything.net/this it's really cool

这些不应该匹配:

http://imgur.com/ixmas.jpg
http://example.com/testdirectory/rawr-gif.gif
testhttp://example.com/rawr

1 个答案:

答案 0 :(得分:2)

使用否定前瞻代替负面后瞻。

\bhttps?://(?!\S+(?:jpe?g|png|bmp|gif))\S+

Live Demo