用于从Content

编程入门 行业动态 更新时间:2024-10-23 10:24:59
本文介绍了用于从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

本文发布于:2023-10-31 19:00:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!