Regex expression for valid website link

前端 未结 2 455
情话喂你
情话喂你 2020-12-31 23:29

In my asp.net application, i need to validate the text for a valid website link. I want to use the regular expression validator for that. Anyone with any idea of how to vali

2条回答
  •  不知归路
    2021-01-01 00:06

    try this -

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

    Explained each step below -

    ^                                   # 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   
    )?$
    

提交回复
热议问题