Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Regex to validate both web and LDAP url

I am creating a Regex ^(http://|https://|ldap://|ldaps://){1}[\S]*$ to validate these types of URL

It should not validate

I will use it for javascript

Upvotes: 1

Views: 1503

Answers (2)

null
null

Reputation: 11879

m{ (?:ldap|http)s?://                   # Scheme

(?:(?!.*\d[/?:]) [a-z0-9\-._~%]+      | # IPv4 host (forbid ending with digit)
   # IP (yep, I've copied this regexp)
   (?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])
   \[[a-z0-9\-._~%!$&'()*+,;=:]+\] ) # or IPv6 host

(?::\d+)?                            # Port

(?:

  [/?]                               # Slash or question mark

  [-A-Z0-9+&@#/%?=~_|$!:,.;]*        # URL itself

)? # Technically, trailing / is required in URLs, but many browser implementations ignore it when accepting those
                                                }ix

This regular expression should work with /ix. As JS doesn't include /x modifier, I'm going to include version without it.

/(?:ldap|http)s?:\/\/(?:(?!.*\d[\/?:])[a-z0-9\-._~%]+|(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(?::\d+)?(?:[\/?][\-A-Z0-9+&@#\/%?=~_|$!:,.;]*)?/i

Note that a lot of characters are allowed in HTTP URLs without escaping using % mark. You probably wouldn't want URL to not pass just because it contains allowed in URLs character * :).

Upvotes: 3

shift66
shift66

Reputation: 11958

for IPs you can use
^(http://|https://|ldap://|ldaps://)((\\d{1,3}\\.){3}\\d{1,3}(:\\d{1,5})?)$
But for other urls... You have to check if site responds. Because it can be a site say wwsw.com and how should your regex know if it's a real site or no? there can be a real site with subdomain wwsw.
EDIT:
GlitchMr is right. For IPv4 use his regex.

Upvotes: 0

Related Questions