Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for urls without http, https, ftp

I'm looking for a regex that accept urls like these:

http://www.example.com
www.example.com

This is what I have so far, but it regex doesn't match URLs without http:// or https://, or ftp://:

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

How can I make the protocol optional?

like image 824
Guest Avatar asked Nov 25 '10 09:11

Guest


3 Answers

Make the (ftp|http|https):\/\/ part optional:

((ftp|http|https):\/\/)?
like image 54
Bart Kiers Avatar answered Sep 23 '22 19:09

Bart Kiers


Try this this will validate url with (http,ftp,https) or without(http,ftp,https)..

/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{3,6}$/;
like image 4
Rajiv Aggarwal Avatar answered Sep 21 '22 19:09

Rajiv Aggarwal


Try this this will validate url with or without(http,ftp,https) in upper and lower cases and also allows you to for numerics

/^(?:(ftp|http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}$/gi;
like image 3
udaykrishna Avatar answered Sep 20 '22 19:09

udaykrishna