Determine if string is in base64 using JavaScript

前端 未结 10 1423
悲哀的现实
悲哀的现实 2020-12-28 14:38

I\'m using the window.atob(\'string\') function to decode a string from base64 to a string. Now I wonder, is there any way to check that \'string\' is actually

10条回答
  •  执笔经年
    2020-12-28 15:19

    This method attempts to decode then encode and compare to the original. Could also be combined with the other answers for environments that throw on parsing errors. Its also possible to have a string that looks like valid base64 from a regex point of view but is not actual base64.

    if(btoa(atob(str))==str){
      //...
    }
    

提交回复
热议问题