Alex Rivera | Logout

Regex for urls without http, https, ftp

Asked 2010-11-25T09:34:32.173
21

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?

Edit
Report

1 Answer

1

Kindly see https://codegolf.stackexchange.com/a/480/6593

Quoting from the above link:

value = 'www.google.com';
if(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi.test(value)) {
            return true;
        } else {
            return false;
        }
answered 2012-10-03T07:24:54.257

Your Answer