簡體   English   中英

用於從 Content-Disposition 標頭中提取文件名的 javascript 正則表達式

[英]javascript regex for extracting filename from Content-Disposition header

Content-disposition 標頭包含可以輕松提取的文件名,但有時它包含雙引號,有時不包含引號,並且可能還有其他一些變體。 有人可以編寫一個適用於所有情況的正則表達式。

Content-Disposition: attachment; filename=content.txt

以下是一些可能的目標字符串:

attachment; filename=content.txt
attachment; filename*=UTF-8''filename.txt
attachment; filename="EURO rates"; filename*=utf-8''%e2%82%ac%20rates
attachment; filename="omáèka.jpg"
and some other combinations might also be there

你可以嘗試這種精神:

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
)

您的文件名在第一個捕獲組中: http//regex101.com/r/hJ7tS6

略微修改以匹配我的用例(刪除所有引號和UTF標記)

filename\\*?=['"]?(?:UTF-\\d['"]*)?([^;\\r\\n"']*)['"]?;?

https://regex101.com/r/UhCzyI/3

/filename[^;=\n]*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/i

https://regex101.com/r/hJ7tS6/51

編輯 :您也可以使用此解析器: https//github.com/Rob--W/open-in-browser/blob/master/extension/content-disposition.js

免責聲明:以下答案僅適用於PCRE (例如Python / PHP),如果您必須使用javascript,請使用Robin的答案。


這個修改后的Robin正則表達式刪除了引號:

filename[^;\n=]*=(['\"])*(.*)(?(1)\1|)

filename        # match filename, followed by
[^;=\n]*        # anything but a ;, a = or a newline
=
(['"])*         # either single or double quote, put it in capturing group 1
(?:utf-8\'\')?  # removes the utf-8 part from the match
(.*)            # second capturing group, will contain the filename
(?(1)\1|)       # if clause: if first capturing group is not empty,
                # match it again (the quotes), else match nothing

https://regex101.com/r/hJ7tS6/28

文件名位於第二個捕獲組中。

這是我的正則表達式。 它適用於Javascript。

filename\*?=((['"])[\s\S]*?\2|[^;\n]*)

我在我的項目中使用了這個。

filename[^;\n]*=(UTF-\d['"]*)?((['"]).*?[.]$\2|[^;\n]*)?

我已經升級了Robin的解決方案,還做了兩件事:

  1. 即使文件已轉義雙引號,也要捕獲文件名。 在此輸入圖像描述

  2. 將UTF-8''部分捕獲為一個單獨的組。 在此輸入圖像描述

這是一個ECMAScript解決方案。

https://regex101.com/r/7Csdp4/3/

我制作了一個使用組filename查找這些名稱的正則表達式

/(?<=filename(?:=|\*=(?:[\w\-]+'')))["']?(?<filename>[^"';\n]+)["']?/g

 const regex = /(?<=filename(?:=|\\*=(?:[\\w\\-]+'')))["']?(?<filename>[^"';\\n]+)["']?/g const filenames = ` attachment; filename=content.txt attachment; filename*=UTF-8''filename.txt attachment; filename="EURO rates"; filename*=utf-8''%e2%82%ac%20rates attachment; filename="omáèka.jpg" ` function logMatches(){ const array = new Array filenames.split("\\n").forEach(line => { if(!line.trim()) return const matches = line.matchAll(regex) const groups = Array.from(matches).map(match => match?.groups?.filename) array.push(groups.length === 1 ? groups[0] : groups) }) console.log(array) } logMatches()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM