cxc-szcx-uniapp/uni_modules/lime-shared/isBase64/index.ts

23 lines
1004 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @ts-nocheck
/**
* 判断一个字符串是否为Base64编码。
* Base64编码的字符串只包含A-Z、a-z、0-9、+、/ 和 = 这些字符。
* @param {string} str - 要检查的字符串。
* @returns {boolean} 如果字符串是Base64编码返回true否则返回false。
*/
export function isBase64(str: string): boolean {
const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
return base64Regex.test(str);
}
/**
* 判断一个字符串是否为Base64编码的data URI。
* Base64编码的data URI通常以"data:"开头后面跟着MIME类型和编码信息然后是Base64编码的数据。
* @param {string} str - 要检查的字符串。
* @returns {boolean} 如果字符串是Base64编码的data URI返回true否则返回false。
*/
export function isBase64DataUri(str: string): boolean {
const dataUriRegex = /^data:([a-zA-Z]+\/[a-zA-Z0-9-+.]+)(;base64)?,([a-zA-Z0-9+/]+={0,2})$/;
return dataUriRegex.test(str);
}