2010-09-16 12 views
5

In meiner asp.net-Anwendung muss ich den Text für eine gültige Website-Link validieren. Ich möchte den Validator für den regulären Ausdruck dafür verwenden. Jeder, der weiß, wie er den Weblink-Benutzerregex validiert.Regex-Ausdruck für gültige Website-Link

Antwort

1
|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i 
+1

scheint nett, aber ich frage mich, nicht alle Websites, beginnt immer mit http oder https? –

9

versuchen, diese -

^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$ 

jeden Schritt weiter unten erläutert -

^         # Start at the beginning of the text 
(?:ftp|http|https):\/\/    # Look for ftp, http, or https 
(?:         # Username:password combinations (optional)  
    [\w\.\-\+]+      # A username  
    :{0,1}        # an optional colon to separate the username and password  
    [\w\.\-\+]*@      # A password 
)? 
(?:[a-z0-9\-\.]+)     # The domain limiting it to just allowed characters 
(?::[0-9]+)?       # Server port number 
(?:         # The path (optional)  
    \/|        # a forward slash  
    \/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)| # or a forward slash followed by a full path  
    \?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+) # or a question mark followed by key value pairs 
)?$ 
+0

Wie wäre es mit Adressen wie twitter.com oder facebook.com? Sieht so aus, als ob dein Geplapper nicht zu ihm passt. – S1awek