Reputation: 97
Negative lookahead to match itself in RegExp
I want to use a RegExp to match an image url that will have two cases:
The first case it is a normal image URL, ending with .png
and may contain some query string, at which point the RegExp will match it in its entirety.
https://test.com/cat.png?pkey=abcd
In the second case, the URL is wrapped in an [img][/img]
tag, the RegExp will be expected not to any string.
[img]https://test.com/cat.png?pkey=abcd[/img]
Now I have written this regular expression, it works great on the first case
(https:\/\/.*?\.png(?:\?[\w&=_-]*)?)(?!\[)
however it does not work with the second case, it will still be matched up to the penultimate character of the URL. How can I modify my RegExp to achieve my goal?
Here's RegExp link: https://regexr.com/7eohf
Upvotes: 3
Views: 75
Reputation: 7880
Try to ancitipate the lookahead immediately after .png
:
(https:\/\/.*?\.png(?!\S*\[)(?:\?[\w&=_-]*)?)
Where \S
matches any character that is not a space (you can replace it as you wish).
See a demo here.
Alternatively, you can impose that the last character matched is not followed by [?\w&=_-]
:
(https:\/\/.*?\.png(?:\?[\w&=_-]*)?)(?![?\w&=_-]|\[)
Upvotes: 3