Getting parts of a URL (Regex)

后端 未结 26 2785
说谎
说谎 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 03:00

    I found the highest voted answer (hometoast's answer) doesn't work perfectly for me. Two problems:

    1. It can not handle port number.
    2. The hash part is broken.

    The following is a modified version:

    ^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$
    

    Position of parts are as follows:

    int SCHEMA = 2, DOMAIN = 3, PORT = 5, PATH = 6, FILE = 8, QUERYSTRING = 9, HASH = 12
    

    Edit posted by anon user:

    function getFileName(path) {
        return path.match(/^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/[\w\/-]+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/i)[8];
    }
    

提交回复
热议问题