Regex to match all valid links

后端 未结 2 1170
野性不改
野性不改 2021-02-06 18:44

In regards to this: http://stackoverflow.uservoice.com/pages/general/suggestions/103227-parser-does-not-match-all-valid-urls is this regex adequate or will it need to be refined

2条回答
  •  渐次进展
    2021-02-06 19:09

    Even though the question is vague, I'll attempt to respond with possible solutions.

    Possible Intention 1: To match any URL's in a given file (for replacement):

    /^([^:]+):\/\/([-\w._]+)(\/[-\w._]\?(.+)?)?$/ig
    

    The above should match nearly all URL formats, with the following captured groups:

    0 => entire match
    1 => protocol (eg. http, ftp, git, ...)
    2 => hostname (eg. www.stackoverflow.com)
    3 => requested_file_path (eg. /images/prod/1/4/success.gif)
    4 => query_string (eg. param=1¶m2=2¶m3=3)
    

    Possible Intention 2: To get details about the current request url

    In order to get details about the url such as the protocol, hostname, requested file path, and query string, you're better off using language/object methods to gather the results. In php you can get all of the above information using function calls:

    $protocol = $_SERVER['SERVER_PROTOCOL']; // HTTP/1.0
    $host = $_SERVER['HTTP_HOST']; // www.stackoverflow.com
    $path_to_file = dirname($_SERVER['SCRIPT_NAME']);
    $file = basename($_SERVER['SCRIPT_NAME']);
    $query_string = $_SERVER['QUERY_STRING'];
    

    Hope this helps in any way.

提交回复
热议问题