javascript regex for extracting filename from Content-Disposition header

后端 未结 6 1535
忘掉有多难
忘掉有多难 2021-01-11 11:58

Content-disposition header contains filename which can be easily extracted, but sometimes it contains double quotes, sometimes no quotes and there are probably some other va

6条回答
  •  清酒与你
    2021-01-11 12:14

    You could try something in this spirit:

    filename[^;=\n]*=((['"]).*?\2|[^;\n]*)
    
    filename      # match filename, followed by
    [^;=\n]*      # anything but a ;, a = or a newline
    =
    (             # first capturing group
        (['"])    # either single or double quote, put it in capturing group 2
        .*?       # anything up until the first...
        \2        # matching quote (single if we found single, double if we find double)
    |             # OR
        [^;\n]*   # anything but a ; or a newline
    )
    

    Your filename is in the first capturing group: http://regex101.com/r/hJ7tS6

提交回复
热议问题