I want to decode a Base64 encoded string, then store it in my database. If the input is not Base64 encoded, I need to throw an error.
How can I check if a string is Base64 encoded?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the following regular expression to check if a string constitutes a valid base64 encoding:
In base64 encoding, the character set is
[A-Z, a-z, 0-9, and + /]. If the rest length is less than 4, the string is padded with'='characters.^([A-Za-z0-9+/]{4})*means the string starts with 0 or more base64 groups.([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$means the string ends in one of three forms:[A-Za-z0-9+/]{4},[A-Za-z0-9+/]{3}=or[A-Za-z0-9+/]{2}==.