Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for only one url validation in javascript

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.

like image 875
priyanka patel Avatar asked Nov 30 '25 21:11

priyanka patel


1 Answers

Try this regex with negative lookahead:

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

Live Demo

like image 134
anubhava Avatar answered Dec 02 '25 13:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!