Regular Expressions 101

Community Patterns

accurate Base64 validation

0

Regular Expression
PCRE (PHP <7.3)

/
^(?:[[:alnum:]+\/]{4})*(?:[[:alnum:]+\/]{2}[AEIMQUYcgkosw048]=|[[:alnum:]+\/][AQgw]==)?$
/
gm

Description

Invalid entries still can be interpreted, e.g.

encode(0xAB) => qw==

however invalid encoding qx== still can be correctly decoded:

decode(qx==) => 0xAB.

If you want invalid encoding to match, use different (less strict) regex.

Replace [:alnum:] with A-Za-z0-9 if your regex library does not support that notation.

Submitted by anonymous - 4 years ago