Getting parts of a URL (Regex)

后端 未结 26 2763
说谎
说谎 2020-11-22 02:13

Given the URL (single line):
http://test.example.com/dir/subdir/file.html

How can I extract the following parts using regular expressions:

  1. The Subd
26条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 02:51

    I needed a regular Expression to match all urls and made this one:

    /(?:([^\:]*)\:\/\/)?(?:([^\:\@]*)(?:\:([^\@]*))?\@)?(?:([^\/\:]*)\.(?=[^\.\/\:]*\.[^\.\/\:]*))?([^\.\/\:]*)(?:\.([^\/\.\:]*))?(?:\:([0-9]*))?(\/[^\?#]*(?=.*?\/)\/)?([^\?#]*)?(?:\?([^#]*))?(?:#(.*))?/
    

    It matches all urls, any protocol, even urls like

    ftp://user:pass@www.cs.server.com:8080/dir1/dir2/file.php?param1=value1#hashtag
    

    The result (in JavaScript) looks like this:

    ["ftp", "user", "pass", "www.cs", "server", "com", "8080", "/dir1/dir2/", "file.php", "param1=value1", "hashtag"]
    

    An url like

    mailto://admin@www.cs.server.com
    

    looks like this:

    ["mailto", "admin", undefined, "www.cs", "server", "com", undefined, undefined, undefined, undefined, undefined] 
    

提交回复
热议问题