How about this?
/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/
Will match: http://domain.com:port/path
or just http://domain
or http://domain:port
/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/
match URLs without path
Some explanations of regex blocks:
Domain: \w+(\.\w+)*
to match text with dots: localhost
or www.yahoo.com
(could be as long as Path or Port section begins)
Port: (:[0-9]+)?
to match or to not match a number starting with semicolon: :8000
(and it could be only one)
Path: \/?(\/[.\w]*)*
to match any alphanums with slashes and dots: /user/images/0001.jpg
(until the end of the line)
(path is very interesting part, now I did it to allow lone or adjacent dots, i.e. such expressions could be possible: /.
or /./
or /.../
and etc. If you'd like to have dots in path like in domain section - without border or adjacent dots, then use \/?(\/\w+(.\w+)*)*
regexp, similar to domain part.)
* UPDATED *
Also, if you would like to have (it is valid) -
characters in your URL (or any other), you should simply expand character class for "URL text matching", i.e. \w+
should become [\-\w]+
and so on.