I’m trying to find a simple regexp for url validation, but not very good in regexing..
Currently I have such regexp: (/^https?://w/).test(url)
So it’s allowing to validate urls as http://localhost:8080 etc.
What I want to do is NOT to validate urls if they have some long special characters at the end like: http://dodo....... or http://dododo&&&&&
Could you help me?
Advertisement
Answer
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.