实现方法
- /**
- * 从base64编码的图片中获取扩展名
- * @param {String} base64
- * @returns
- */
- function getExtensionFromBase64(base64) {
- let re = new RegExp('data:image/(?<ext>.*?);base64,.*')
- let res = re.exec(base64)
- if (res) {
- return res.groups.ext
- }
- }
-
测试
- let base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEU'
- let ext = getExtensionFromBase64(base64)
- console.log(ext)
- // png
-