📜  grepper 有效 base64 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:07.529000             🧑  作者: Mango

代码示例1
// Check if string is Base64 encoded
// Source answer provided by: https://stackoverflow.com/users/549471/anders-marzi-tornblad

const b64Validator = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;

if(b64Validator.test(stringToTest) {
  // etc
}

// Explanation:
/*
    ^                          # Start of input
    ([0-9a-zA-Z+/]{4})*        # Groups of 4 valid characters decode
                               # to 24 bits of data for each group
    (                          # Either ending with:
        ([0-9a-zA-Z+/]{2}==)   # two valid characters followed by ==
        |                      # , or
        ([0-9a-zA-Z+/]{3}=)    # three valid characters followed by =
    )?                         # , or nothing
    $                          # End of input
*/