user2010925
user2010925

Reputation:

Regex to match URL's not ending in jpg or png

So here is what I got so far:

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

Problem is, it's matching this:

http://imgur.com/test/img.jpg (Up to the img). I want it to return no matches.

So basically, these should match:

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

And these shouldn't match:

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

Upvotes: 0

Views: 2161

Answers (1)

hwnd
hwnd

Reputation: 70750

Use Negative Lookahead instead of Negative Lookbehind.

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

Live Demo

Upvotes: 4

Related Questions