kevva / base64-regex

Regular expression for matching base64 encoded strings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regex doesn't validate base64 not ending in =

cjbarth opened this issue · comments

If you look at the regex, you'll see that it requires a segment that ends in a =. For example, QmCC is a valid base64 string, but this regex doesn't validate that string:

https://regex101.com/r/uGf7ne/1

However, ^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$ allows the last segment to not have to end in an =.

https://regex101.com/r/uGf7ne/2

I might also add that I've seen other people lift this regex for other uses (https://stackoverflow.com/a/29275028), which I'm also doing, thus I found this problem. In that case, it might be helpful to the greater community to make the regex more 'flavor compatible' by escaping some characters that other languages require, like so: ^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$

I'd be happy to create a PR if so desired.

Nevermind. I wasn't looking closely at how the regex is mutated before being used...