0

I've following regex to match urls:

/(https?:\/\/[^"<\s]+[^.<\s"()][-A-Za-z0-9+&@#\/%=~_|])(?![^<>]*>|[^"]*?<\/a)/gi

Who match both urls:

http://example.fr/image.png
http://example.fr/image.pdf

This is a specific regex to our application, I want to keep it as it, but just add one functionality.

Question: I want to ignore urls ending with .png.

Is it possible?

1 Answer 1

2

Yes, it is possible.

Use a negative lookbehind at the end, and force it to match the entire string:

(https?:\/\/[^"<\s]+[^.<\s"()][-A-Za-z0-9+&@#\/%=~_|])(?![^<>]*>|[^"]*?<\/a)(?<!\.png)$

Try it online

The negative lookbehind (?<!\.png) is a zero-width match if the current position isn't preceeded by something matching \.png. $ forces the regex to match the entire line.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, problem is I can have characters after the url, see regex101.com/r/lMCWPP/1
Then change the $ into a character set of values definitely not in the URL: regex101.com/r/lMCWPP/2
Thanks, what if I don't want to match the space after the end of the url?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.