5

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?

Kosmetika
  • 19,249
  • 37
  • 94
  • 154
  • possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Kosmetika Sep 09 '13 at 16:47

5 Answers5

12

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.

rook
  • 4,687
  • 3
  • 34
  • 47
2

It depends on how complex you need the Regex to be. A simple way would be to just accept words (and the port/domain):

^https?:\/\/\w+(:[0-9]*)?(\.\w+)?$

Remember you need to use the + character to match one or more characters.

Of course, there are far better & more complicated solutions out there.

Community
  • 1
  • 1
CodingIntrigue
  • 65,670
  • 26
  • 159
  • 166
2

If you want to match ABCD then you may leave the start part..

For Example to match http://localhost:8080

' just write

/(localhost).

if you want to match specific thing then please focus the term that you want to search, not the starting and ending of sentence.

Regular expression is for searching the terms, until we have a rigid rule for the same. :)

i hope this will do..

MarmiK
  • 5,320
  • 6
  • 34
  • 44
0
^https?:\/\/localhost:[0-9]{1,5}\/([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)

match:

  • https://localhost:65535/file-upload-svc/files/app?query=abc#next

not match:

  • https://localhost:775535/file-upload-svc/files/app?query=abc#next

explanation

  • it can only be used for localhost
  • it also check the value for port number since it should be less than 65535 but you probably need to add additional logic
ExploreEv
  • 375
  • 4
  • 7
-2
^https?:\/\/(localhost:([0-9]+\.)+[a-zA-Z0-9]{1,6})?$

Will match the following cases :

Will NOT match the following cases :

Chirag Maliwal
  • 400
  • 10
  • 21