Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp javascript - url match with localhost

I 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?

like image 790
Kosmetika Avatar asked Sep 09 '13 11:09

Kosmetika


People also ask

Can we use regex in URL?

URL regular expressions can be used to verify if a string has a valid URL format as well as to extract an URL from a string.

How do I find the regex for a URL?

Match the given URL with the regular expression. In Java, this can be done by using Pattern. matcher(). Return true if the URL matches with the given regular expression, else return false.

How do you match in regex?

2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .


1 Answers

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.

like image 182
rook Avatar answered Sep 28 '22 02:09

rook