0

Possible Duplicate:
RegEx to parse or validate Base64 data

How to validate Base64 string in Javascript using regex?

Community
  • 1
  • 1
Parag Meshram
  • 7,580
  • 8
  • 47
  • 86
  • Why don't you try decoding it? `function validate(str) { try { atob(str); return true; } catch (e) { return false; }}` – Blender Oct 17 '12 at 08:25

1 Answers1

2

See if this answer fits you, from another user:

^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$
Community
  • 1
  • 1
Armaggedon
  • 379
  • 4
  • 11
  • This regex will cause a `RangeError: Maximum call stack size exceeded` with some large base64 strings (like encoded images) so it may be better to use something like https://bit.dev/chriso/validator-js/is-base64/~code to avoid having unexpected errors. – Santiago G. Marín Nov 11 '20 at 13:06