Regex for base64

/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm

Options
Matches

Examples

Note

Checks the alphabet and padding. Plenty of ordinary words are valid Base64 too, so a match is not proof.

Matches in the example

  • "aGVsbG8="
  • "SGVsbG8sIHdvcmxkIQ=="

Token by token

  1. ^ the start of the string (or line, with the m flag)
  2. (?: a group, not capturing
  3. [A-Za-z0-9+/]{4} one of: “A” to “Z”, “a” to “z”, “0” to “9”, “+” or “/”, exactly 4 times
  4. )* end of group, whole group zero or more times, as many as possible
  5. (?: a group, not capturing
  6. [A-Za-z0-9+/]{2} one of: “A” to “Z”, “a” to “z”, “0” to “9”, “+” or “/”, exactly 2 times
  7. == the text “==”
  8. | or
  9. [A-Za-z0-9+/]{3} one of: “A” to “Z”, “a” to “z”, “0” to “9”, “+” or “/”, exactly 3 times
  10. = the character “=”
  11. )? end of group, whole group optionally
  12. $ the end of the string (or line, with the m flag)

Other patterns