3

at present i am using following regular expression

/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/

it validating url like https://scores.arrowsight.com/scores/ORoom/Hospital%20A1/Hospital%20A2 but it should not validate url like http://www.google.co.in/http://www.google.co.in/ above url should not allow but it allow http://www.google.co.in/http://www.google.co.in/ Can someone help me in modifying this? Thanks in advance.

4
  • Aside from needing to be URL encoded, that URL is valid. If you put it in a browser, it would work. Do you only want to allow URL safe characters? Commented Aug 2, 2013 at 6:40
  • ya that is valid but i want validation for only one url should be there not two urls... Commented Aug 2, 2013 at 7:04
  • Technically, that is one URL, but I understand what you mean. Commented Aug 2, 2013 at 7:08
  • @priyankapatel: There aren't two URLs. That URL is totally valid. Commented Aug 2, 2013 at 7:46

1 Answer 1

1

Try this regex with negative lookahead:

/^(ftp|https?):\/\/(\w+:{0,1}\w*@)?((?![^\/]+\/(?:ftp|https?):)\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/

Live Demo

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

2 Comments

It can be modified a bit to disallow (?:ftp|https?) anywhere after first slash.

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.