正则表达式解析图像数据URI
如果我有:
<img src="data:image/gif;base64,R0lGODlhtwBEANUAAMbIypOVmO7v76yusOHi49AsSDY1N2NkZvvs6VVWWPDAutZOWJ+hpPPPyeqmoNlcYXBxdNTV1nx+gN51c4iJjEdHSfbc19M+UOeZk7m7veSMiNtpauGBfu2zrc4RQSMfIP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAC3AEQAAAb/QJBwSCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs" />
如何将数据部分解析为:
- Mime 类型(image/gif)
- 编码(base64)
- 图像数据(二进制数据)
If I have :
<img src="data:image/gif;base64,R0lGODlhtwBEANUAAMbIypOVmO7v76yusOHi49AsSDY1N2NkZvvs6VVWWPDAutZOWJ+hpPPPyeqmoNlcYXBxdNTV1nx+gN51c4iJjEdHSfbc19M+UOeZk7m7veSMiNtpauGBfu2zrc4RQSMfIP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAC3AEQAAAb/QJBwSCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs" />
How can I parse the data part into:
- Mime type (image/gif)
- Encoding (base64)
- Image data (the binary data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:扩展以显示用法
注意:正则表达式适用于有问题的输入。如果还指定了
charset
,它将不起作用并且必须重写。EDIT: expanded to show usage
NOTE: The regex applies to the input shown in question. If there was a
charset
specified too, it would not work and would have to be rewritten.实际上,您不需要正则表达式。根据 Wikipedia,数据 URI 格式
只需执行以下操作:
Actually, you don't need a regex for that. According to Wikipedia, the data URI format is
so just do the following:
我还面临着解析数据 URI 方案的需要。因此,我改进了本页上专门针对 C# 给出的正则表达式,它适合任何数据 URI 方案(要检查该方案,您可以从 此处 或此处。
这里是我的 C# 解决方案:
I faced also with the need to parse the data URI scheme. As a result, I improved the regular expression given on this page specifically for C# and which fits any data URI scheme (to check the scheme, you can take it from here or here.
Here is my solution for C#:
数据 URI 对它们来说有点复杂,它们可以包含参数、媒体类型等……有时您需要知道这些信息,而不仅仅是数据。
要解析数据 URI 并提取所有相关部分,请尝试以下操作:
这将为您提供一个对象,该对象已解析出所有相关位,并且参数为字典 {foo: baz}。
示例(带断言的摩卡测试):
Data URI's have a bit of complexity to them, they can contain params, media type, etc... and sometimes you need to know this info, not just the data.
To parse a data URI and extract all of the relevant parts, try this:
This will give you an object that has all the relevant bits parsed out, and the params as a dictionary {foo: baz}.
Example (mocha test with assert):
这是我的正则表达式,我还必须分离 mime 类型(图像/jpg)。
Here is my regular expression where I had to separate the mime-type (image/jpg) as well.