本文介绍了用于从Content-Disposition标头中提取文件名的javascript正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Content-disposition header包含可以轻松提取的文件名,但有时它包含双引号,有时没有引号,也可能还有其他一些变体。有人可以编写一个适用于所有情况的正则表达式。
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 variants too. Can someone write a regex which works in all the cases.
Content-Disposition: attachment; filename=content.txt以下是一些可能的目标字符串:
Here are some of the possible target strings:
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 )您的文件名在第一个捕获组中: regex101/r/hJ7tS6
Your filename is in the first capturing group: regex101/r/hJ7tS6
更多推荐
用于从Content
发布评论