0

I'm using this reg ex

var regex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;

This is used on a field for specifying image urls, so I need the last part of the url to be either .jpg, .png, .gif etc

How can i modify the regex to test this?

Thanks

(regex used: http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the)

1
  • This pattern is long and inefficient! Matching the protocol with [A-Za-z]{3,9} is a joke! Forget it and write your own. Commented Dec 20, 2013 at 17:59

1 Answer 1

1

Try appending your existing regex with \.(jpg|png|gif)

Basically what this does is it checks for a dot (\., must be escaped since its a special character), then checks if a string is followed by either jpg, png, or gif.

Debuggex is a wonderful tool to play around with (I learned regex messing with it).

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)\.(jpg|png|gif)

Regular expression visualization

Debuggex Demo

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

Comments

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.